Allow the computer to be the number guesser. Your program will ask you for a num
ID: 3546924 • Letter: A
Question
Allow the computer to be the number guesser. Your program will ask you for a number
between 1 and 100. The computer will pick a number and you will tell the computer if the number you've chosen is higher
or lower then the number guessed (Try not to lie to the computer player. You wouldn't lie to a computer, would you?). The
program halts when the computer player guesses the number you've chosen or after the computer player has guessed
unsuccessfully 10 times.
Your program should have a fairly intelligent computer player. That is, if you have informed the computer player that the
number they have chosen is above the correct number, the computer player should never guess a number above that.
Likewise, if you have informed the computer player that its guess is below the correct number, it should never guess below
that number. Within these conditions, the computer may choose a random number as necessary.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int main()
{int number,hi=101,lo=1,guesses=0;
char hilo='k';
cout<<"Think of a number between 1 and 100 ";
do
{guesses++;
number=(hi+lo)/2;
cout<<"Is it a "<<number<<"?(enter h for too high, l for too low, c for correct): ";
cin>>hilo;
if(hilo=='h')
hi=number;
else if(hilo=='l')
lo=number;
}while(hilo != 'c'&&guesses<10);
if(guesses==10)
cout<<"Whaa, I didn't guess the number ";
else
cout<<"Yeah, I guessed it ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.