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.
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.
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.
This is a very short trivia game. See if the user can guess the year that...
This is a very short trivia game. See if the user can guess the year that MDC (formerly Dade County Junior College) first started offering classes (1960). If the user guesses incorrectly, ask if they want another attempt. Once the user gets it right, report the number of guesses needed and end the program. Initial Output: MDC was originally named Dade County Junior College. Output (per loop) In what year were classes first offered? [user types: 1980] That answer is...
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5
Write a program that asks the user to enter an odd number and prints out a...
Write a program that asks the user to enter an odd number and prints out a triangle pattern. For example, if the user enters 5, the output is note: There is one space between the numbers in each line 1 3 5 1 3 1 If the user enters 9 the output is: 1 3 5 7 9 1 3 5 7 1 3 5 1 3 1 program language: C++
Write a function in Matlab that takes as input the number of iterations k, the size...
Write a function in Matlab that takes as input the number of iterations k, the size n, and a sparse matrix A. Have this function run the Power method for k iterations on an initial guess the vector of 1’s and output the dominant eigenvalue and its corresponding eigenvector. Use only basic programming. Write out or print out your function.
Write a C++ program with a user-defined function myFactorial, that calculates the factorial of a number...
Write a C++ program with a user-defined function myFactorial, that calculates the factorial of a number entered by the user. Return the calculated factorial and print it in the main function.
c program with notes Start by asking the user to enter a number between 100 and...
c program with notes Start by asking the user to enter a number between 100 and 10,000. Check if the user has complied. Keep asking the user for a number between 100 and 10,000 until the user complies.Then print on the screen the digits of that number in reverse order
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT