(Guess-the-Number Game Modification) Modify the program of Exercise 5.34 to count the number of guesses the player makes. If the number is 10 or fewer, print "Either you know the se- cret or you got lucky!" If the player guesses the number in 10 tries, then print "Ahah! You know the secret!" If the player makes more than 10 guesses, then print "You should be able to do better!" Why should it take no more than 10 guesses? Well, with each “good guess” the player should be able to eliminate half of the numbers. Now show why any number from 1 to 1000 can be guessed in 10 or fewer tries.

What will be an ideal response?


```
// Randomly generate numbers between 1 and 1000 for user to guess;
// analyze number of guesses before correct response.
#include
#include
#include
using namespace std;

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

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

// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame()
{
int answer; // randomly generated number
int guess; // user's guess
int total; // number of guesses
char response; // 'y' or 'n' response to continue game

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

// prompt for guess
cout << "I have a number between 1 and 1000.\n"
<< "Can you guess my number?\n"
<< "Please type your first guess." << endl << "? ";
cin >> guess;
total++;

// loop until correct number
while ( !isCorrect( guess, answer ) )
{
cin >> guess;
total++;
} // end while

cout << "\nExcellent! You guessed the number!\n";
analyzeCount( total );

// prompt for another game
cout << "Would you like to play again (y or n)? ";
cin >> response;
cout << endl;
} while ( response == 'y' );
} // end function guessGame

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

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

return false;
} // end function isCorrect

// analyzeCount determines if user knows "secret"
void analyzeCount( int count )
{
if ( count < 10 )
cout << "Either you know the secret or you got lucky!\n";
else if ( count == 10 )
cout << "Ahah! You know the secret!\n";
else
cout << "You should be able to do better!\n\n";
} // end function analyzeCount
```

Computer Science & Information Technology

You might also like to view...

You can use _____ type(s) of graphics to enhance documents created with Microsoft Word.

A. one B. three C. two D. many

Computer Science & Information Technology

A(n) ________ is a moving series of pictures or images that displays when your compute has been idle for a specified period of time

Fill in the blank(s) with correct word

Computer Science & Information Technology

You are in the process of upgrading a network from 100-megabit Ethernet to Gigabit Ethernet. Explain what you will need to do when you connect a computer with a 100-megabit/Gigabit Ethernet NIC to a 100-megabit/Gigabit switch.

What will be an ideal response?

Computer Science & Information Technology

When you enter text in a bulleted list placeholder, it is automatically formatted as a Level 2 bullet. _________________________

Answer the following statement true (T) or false (F)

Computer Science & Information Technology