Write a program with a method that plays the guess a number game. The program sh
ID: 3692376 • Letter: W
Question
Write a program with a method that plays the guess a number game. The program should allow the user to pick a number between 1 and 1000 in his head. The method should guess the user's number in a minimal amount of attempts. The method should ask the user "is the number greater than or less than the number " and the program gives a particular number. The user in some way just inputs (higher or lower) or (greater than or less than). There also has to be a way for the user to say the number has just been guessed. Of course, the user cannot answer incorrectly or change the number midstream. Note - if written efficiently (and for passing credit), the method should be able to guess any number in 10 or less attempts. The final date/time to submit this assignment is Friday, April 22nd at midnight.Explanation / Answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
int start=1,end=1000;
int mid;
cout<<"Please select a number and remember it."<<endl;
char ch;
while(start<=end)
{
mid = (start+end)/2;
cout<<"Is "<<mid<<" the number?(Enter Y/N):";
cin>>ch;
if(ch=='Y')
{
cout<<"The number is "<<mid<<endl;
break;
}
cout<<"Is "<<mid<<" greater than or lesser than the number?(Enter G for greater/L for lesser):";
cin>>ch;
if(ch=='G')
{
end=mid;
}
else
{
start=mid;
}
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.