#include #include #include using namespace std; void guessGame(); // function prototype bool isCorrect( int, 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 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; // 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; // loop until correct number while ( !isCorrect( guess, answer ) ) cin >> guess; // prompt for another game cout << "\nExcellent! You guessed the number!\n" << "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 ) { // guess is correct if ( g == a ) 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 Modify the code using the instructions in the picture.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

C++

6.34

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void guessGame(); // function prototype
bool isCorrect( int, 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
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;

// 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;

// loop until correct number
while ( !isCorrect( guess, answer ) )
cin >> guess;

// prompt for another game
cout << "\nExcellent! You guessed the number!\n"
<< "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 )
{
// guess is correct
if ( g == a )
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

Modify the code using the instructions in the picture.

### Guess-the-Number Game Modification

In this exercise, you will modify the program from **Exercise 6.34** to count the number of guesses a player makes.

- If the player guesses the number in **10 or fewer tries**, display the message:
  > "Either you know the secret or you got lucky!"

- If the player guesses the number in exactly **10 tries**, display:
  > "Ahah! You know the secret!"

- If the player takes **more than 10 guesses**, display:
  > "You should be able to do better!"

#### Explanation:
The task is to ensure guessing the number takes no more than 10 attempts. With each effective guess, the player should be able to eliminate half of the potential numbers. This strategy demonstrates that any number between 1 and 1000 can be accurately guessed within 10 tries, leveraging the process of elimination through progressively informed guesses.
Transcribed Image Text:### Guess-the-Number Game Modification In this exercise, you will modify the program from **Exercise 6.34** to count the number of guesses a player makes. - If the player guesses the number in **10 or fewer tries**, display the message: > "Either you know the secret or you got lucky!" - If the player guesses the number in exactly **10 tries**, display: > "Ahah! You know the secret!" - If the player takes **more than 10 guesses**, display: > "You should be able to do better!" #### Explanation: The task is to ensure guessing the number takes no more than 10 attempts. With each effective guess, the player should be able to eliminate half of the potential numbers. This strategy demonstrates that any number between 1 and 1000 can be accurately guessed within 10 tries, leveraging the process of elimination through progressively informed guesses.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education