Write a Guess My Number Game program The program generates a

Write a \"Guess My Number Game\" program. The program generates a random integer in a specified range and the user (the player has to guess the number. The program allows the user to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again. The basic algorithm is as follows. The program starts by printing instructions on the screen. For every game: the program generates a new random integer in the range from MIN to MAX. Treat MIN and MAX like constants; start by initializing them to 1 and 100 loop to prompt the player for a guess until the player correctly guesses the integer for each guess, the program prints whether the player\'s guess was too low, too high, or correct. At the conclusion (when the integer has been guessed): print the total number of guesses for that game. print a message regarding how well the player did in that game (e.g., the player took way too long to guess the number, the player was awesome etc); to do this, you will have to decide on ranges for your messages and Give a rationale for your decision in a comment in the program. After all games have been played, print a summary showing the average number of guesses.

Solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{

int random = 0;
int guessed = 0;
int counter = 0;

srand(time(NULL));
random = rand() % 100 + 1;

printf(\"Guess my number! \");

while(1)
{
counter++;

scanf(\"%d\", &guessed);

if (guessed == random)
{
printf(\"You guessed correctly in %d tries! Congratulations!\ \", counter);

if(counter <5) //if he answers within 5 guess
{
printf(\"You have done awesome \");
}
else if(counter>5) //if he took more than 5 guesses
{
printf(\"You tooke to long \");
}
break;
}

if (guessed < random)
printf(\"Your guess is too low. Guess again. \");

if (guessed > random)
printf(\"Your guess is too high. Guess again. \");

}

return 0;   
}

============================================

./a.out
Guess my number! 23
Your guess is too low. Guess again. 50
Your guess is too low. Guess again. 75
Your guess is too high. Guess again. 74
Your guess is too high. Guess again. 65
Your guess is too high. Guess again. 64
Your guess is too high. Guess again. 55
Your guess is too low. Guess again. 57
Your guess is too high. Guess again. 56
You guessed correctly in 9 tries! Congratulations!
You tooke to long

 Write a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site