Question

In: Computer Science

we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000...

we saw a C program called GuessNumber.c that generates a random integer between 1 and 1000 and asks the user to guess that number. Modify the program so that at the end of each round, it also prints the number of guesses made by the user to reach the answer as well as the best score so far, i.e., the minimum number of guesses used in any round since the program was started. The source code of the original version of GuessNumber.c can be found in: https://github.com/pdeitel/CHowToProgram8e/archive/master.zip under examples/ch01/GuessNumber/GNU/randomized_version/

Solutions

Expert Solution

// Randomly generate numbers between 1 and 1000 for user to guess.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void guessGame(void); // function prototype
int isCorrect(int, int); // function prototype

int main(void)
{
srand(time(0)); // seed random number generator
guessGame();
} // end main

// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame(void)
{
int answer; // randomly generated number
int guess; // user's guess
int response; // 1 or 2 response to continue game
int min=100000;
int guesses=0;

// loop until user types 2 to quit game
do {
// generate random number between 1 and 1000
// 1 is shift, 1000 is scaling factor
guesses = 0;
answer = 1 + rand() % 1000;

// prompt for guess
puts("I have a number between 1 and 1000.\n"
"Can you guess my number?\n"
"Please type your first guess.");
printf("%s", "? ");
scanf("%d", &guess);

// loop until correct number
while (!isCorrect(guess, answer))
{
scanf("%d", &guess);
guesses++;
}

if( guesses < min )
min = guesses;

printf("\n The number of guesses taken by the user is %d", guesses);
printf("\n Best Score So far: %d", min);

// prompt for another game
puts("\nExcellent! You guessed the number!\n"
"Would you like to play again?");
printf("%s", "Please type ( 1=yes, 2=no )? ");
scanf("%d", &response);

puts("");
} while (response == 1);
} // end function guessGame

// isCorrect returns true if g equals a
// if g does not equal a, displays hint
int isCorrect(int g, int a)
{
// guess is correct
if (g == a)
return 1;

// guess is incorrect; display hint
if (g < a)
printf( "%s", "Too low. Try again.\n? " );
else
printf( "%s", "Too high. Try again.\n? " );

return 0;
} // end function isCorrect


Related Solutions

Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive)....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive). Note: the tests call your function 1000 times to verify that no results are <0 or >1000. Rarely, this can score an incorrect function as correct.
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT