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

1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12 and displays the month name Jan, Feb, …, December for the number 1, 2, …, 12, accordingly.. Sample Run: 9 Oct 2- Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi Riyals. Prompt the user to enter 0 to convert from U.S. dollars to Saudi Riyals and 1 to convert from Saudi Riyal and...
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...
Write a program in C++ that generates a random number between 1 and 10 and asks...
Write a program in C++ that generates a random number between 1 and 10 and asks the user to guess it. Your program should continue until the user guesses the correct number. With each guess the user makes the program tells the user if the guess is too high or too low. To generate a random number between 1 and 10 you need the following code: /* initialize random seed: */ srand (time(NULL)); /* generate secret number between 1 and...
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....
who to write a program c++ that reads in an integer between 0 and 1000 and...
who to write a program c++ that reads in an integer between 0 and 1000 and adds all the digits in the integer?
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT