Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to generate these three random integers. In each iteration, generate and display one random integer. If it is larger than the computer’s pick, display "You have won the game" and break out of the loop. If the human player cannot get a random integer larger than computer’s pick after three iterations , display "sorry you have lost the game".

Respuesta :

Answer:

int i, a, w;

w = 0;

a = 6 + rand()%4;

for(i = 0; i < 3; i++){

 b = 1 + rand()%9;

if(b > a){

   w = 1;

   printf("You have won the game");

   break;

}

}

if(w == 0)

  printf("You have lost the game\n);

Explanation:

I am going to write a C program.

First, generate a random integer in the range of 6 through 9.

We can have 4 values for the random integers: 6,7,8 or 9

How can I generate a random integer in the range of 6 through 9?

I can have a variable a = 6(the lowest possible value for the randon integers).

Then, i am going to call the rand() function of C. This function returns a random value; I am going to add a random value with a. To be in the range, this value has to be smaller than 4. So i am going to find the modulus of this value by 4. So I have: a = 6 + rand()%4;

Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to generate these three random integers. In each iteration, generate and display one random integer. If it is larger than the computer’s pick, display "You have won the game" and break out of the loop.

To solve this, i am going to need a flag variable, so the program knows what message to call(If the player won or lost).

I am going to call this variable int w; This will start at 0, and if the player wins it is set to 1;

The for loop is from i = 0 to 2, since the player has three chances. Inside the loop, for each iteration, a random number is going to be generated, using the same process as to generate the computer's  random value;

Then the if clause compares the player's generated value with the computer's. If the player's value is bigger, the flag is updated, the message is shown and the command break breaks the program out of the loop; If the player's value is lower, and the player has another chance, we go back to the generation, at b = 1 + rand()%9; If three numbers have already been generated, the player has lost and the program also breaks out of the loop.

After the loop, if the value of w(the flag) has not been changed, the loss message is displayed.

ACCESS MORE