Question

Write a program that plays a number guessing game with a human user. The human user...

Write a program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number.

sample run:

enter number to be guessed:88

output: you entered 88, and it took the program 3 iterations to guess.

Homework Answers

Answer #1
#include <iostream>
using namespace std;

const int MAX_VALUE = 100;
const int MIN_VALUE = 0;

int guess;
int high = MAX_VALUE;
int low = MIN_VALUE;

char choice;

int main(){


cout<<"Think about a number between "<<MIN_VALUE<<" and "<<MAX_VALUE<<". \n\n";
guess = ( high-low ) / 2;

while(guess>low){
    cout<<"Is your number less than or equal to "<<guess<<"? \nEnter y or n. \n\n";
    cin>>choice;

    if(choice=='y' || choice=='Y') {
        high = guess;
        if(high-low==1) guess=low;
        else guess -= ( high - low ) / 2;
    }
    else if(choice=='n' || choice=='N') {
        low = guess;
        guess += (high - low ) /2;
    }
    else cout<<"Incorrect choice."<<endl;


}
cout<<"Your number is: "<<high<<".\n";

return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write...
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write a program where the user and the program/computer play a number guessing game. The program should prompt the user for a number (between 1 and 100, inclusive) then the program/computer has to guess what the user entered. Keep track of the number of iterations it takes for the computer to guess the number. Sample runs are shown below: Enter number to be guessed: 88...
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
Write a script that plays a simple “guess the number” game with the user. It should...
Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a program in Java where a user of this program will play a game in...
Write a program in Java where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Python programming exercise 3.3 (guess.py) Modify the guessing-game program so that the user thinks of a...
Python programming exercise 3.3 (guess.py) Modify the guessing-game program so that the user thinks of a number that the computer must guess.
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Write a c++ program that has the user guess a number between 1 and 100, you...
Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT