-
Need Help!!
I am trying to write a game where a person thinks of a digit between 1 and 100 and computer has to guess it by asking you questions which you can only repond by typing '>', '<', or '='. What I wrote so far works only if the digit you think is 100 or 99. I can continue adding more lines in the program so that it covers all digits between 1 and 100, but seems like there should be a more efficient way of accomplishing this problem. If anyone knows of a better and faster way of doing it, please HELP. I will deeply appreciate that. Thanks a lot.
Arooj. Here is what I wrote:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char ans;
cout << "Think of a number between 1 and 100 and I will\n"
"tell you the number that you thought.\n";
cout << "\nStrike any key when you are ready . . . . .";
getch();
clrscr();
cout << "\n\nOk, I am going to ask you a few questions and I just want you to\nrespond by entering '>' '<' or '=' as your answer.\n\n";
cout << "Is this number greater than, equal to, or or less than "
<< 50? ";
cin >> ans;
if (ans == '>')
{
cout << "\nIs the number greater than, equal to, or less than " << (51 + 100 / 2) << ": " ;
cin >> ans;
if (ans == '>'){
cout << "\nIs the number greater than, equal to, or less than " << (76 + 100) / 2 << ": " ;
cin >> ans;
if (ans == '>')
cout << "\nIs the number greater than, equal to, or less than " << (88 + 100) / 2 << ": " ;
cin >> ans;
if (ans == '>')
cout << "\nIs the number greater than, equal to, or less than " << (94 + 100) / 2 << ": " ;
cin >> ans;
if (ans == '>')
cout << "\nIs the number greater than, equal to, or less than " << (97 + 100) / 2 << ": " ;
cin >> ans;
if (ans == '>')
cout << "\nIs the number greater than, equal to, or less than " << (98 + 100) / 2 << ": " ;
cin >> ans;
if (ans == '>')
cout << "The number is 100. ";
else
cout << "The number is 99. ";
}
}
getch();
}
-
You have the right train of though. Just think out the binary search a bit more. Use a loop to make it more efficient, so to speak.
-
The easiest thing for you to do is what is know as a binary search.
range 0-100. midpoint 50
is number < 50
if yes try 25
if no try 75
is number < 25 | 75
if yes try 13|67
if no try 37|87
etc.
eventually u will find number in a smallish number of comparisons.
-
not too long ago i tried to do that exact same thing
http://cboard.cprogramming.com/showt...threadid=35020
-
Thanks all very very much. I followed the advice and came up with a better code than what I had written before.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int low = 0, high = 100, mid = (low + high)/2;
char ans = '0';
cout << "Think of a number between 1 and 100 and I will\n"
"tell you the number that you thought.\n";
cout << "\nStrike any key when you are ready . . . . .";
getch();
clrscr();
cout << "\n\nOk, I am going to ask you a few questions and I just "
"want you to\nrespond by entering '>' '<' or '=' as your answer.\n\nIf"
" by mistake you enter incorrect answer, type = to end the game\n"
"and start all over again.\n\n";
while ( ans != '=') {
cout << "\nIs it greater than, equal to or less than: " << mid << endl;
cin >> ans;
if (ans == '>')
low = mid +1;
else
if (ans != '=')
high = mid;
mid = (low + high)/2;
}
cout << "\nThe answer is: " << mid;
getch();
}