Hey guys,
I need to Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial guess). The user responds with 'O’ for older', ‘Y for 'younger' and ‘X’ for ‘you got it!’
I should use a while loop to ask user whether his/her age is ‘>’, ’<’ or ‘=’ the guess,and update the guess using the binary search method. Note that I'm not allowed to use breaks.
Here's my code so far, I'm not sure how to continue:
You should not be using random numbers for this. The computer should not guess randomly, but instead should "divide and conquer" to determine the user's age.
That is.. you want to guess halfway between the known minimum and maximum. The user tells you if they are older/younger, which lets you establish a new min/max.
Example:
- Starting min/max is [1,128]
- computer guesses 64 (halfway between 1,128)
- player enters 'Y', you now know the new max is 63. New range: [1,63]
- computer guesses 32 (halfway).
- player enters 'Y'. New range: [1,31]
- computer guesses 16
- player enters 'O'. New range: [17,31]
- computer guesses 24
etc
etc
With that pattern, the computer will be able to correctly determine the user's age in no more than 7 guesses.
#include <iostream>
#include <string>
usingnamespace std;
constint NUMBER_OF_GUESSES = 7;
int main()
{
int Guess;
int numGuesses = 1;
char answer;
int max;
int min;
cout << "This program will try to guess your age in under" <<endl;
cout << "7 attempts." << endl << endl;
cout << "Press \'o\' if the number is too high" <<endl;
cout << " or \'y\' if the number was too low, or \'x\'" << endl;
cout << " if the number was correct."
<< endl << endl;
Guess = 64;
while (numGuesses <= 7)
{
cout << "Are you " << Guess << " years old?" << endl;
cin >> answer;
if (answer == 'o')
{
max = 128;
min = Guess + 1;
Guess = (max + min)/2;
numGuesses ++;
}
elseif (answer == 'y')
{
max = Guess - 1;
min = 1;
Guess = (max + 1)/2;
numGuesses ++;
}
if (answer == 'x')
{
cout << "Your age is: " << Guess
<< ". That was lucky!"
<< endl;
}
}
}
#include <iostream>
#include <string>
usingnamespace std;
constint NUMBER_OF_GUESSES = 7;
int main()
{
int Guess;
int numGuesses = 1;
char answer;
int max;
int min;
cout << "This program will try to guess your age in under" <<endl;
cout << "7 attempts." << endl << endl;
cout << "Press \'o\' if the number is too high" <<endl;
cout << " or \'y\' if the number was too low, or \'x\'" << endl;
cout << " if the number was correct."
<< endl << endl;
Guess = 64;
while (numGuesses <= 7)
{
cout << "Are you " << Guess << " years old?" << endl;
cin >> answer;
if (answer == 'o' || 'O')
{
max = 128;
min = Guess + 1;
Guess = (max + min)/2;
numGuesses ++;
}
elseif (answer == 'y' || 'Y')
{
max = Guess - 1;
min = 1;
Guess = (max + 1)/2;
numGuesses ++;
}
if (answer == 'x' || 'X')
{
cout << "Your age is: " << Guess
<< ". That was lucky!"
<< endl;
}
}
}
Thanks guys, but I'm still having trouble with the program.
Here's an example of what happens:
computer guesses: 64
user enters: y
computer: 32
user enters:o
computer: 80
how do i fix this? i tried setting max= guess, didn't work.
computer guesses 64
player says Y
computer guesses 32
player says O
With this, your code would reset the max to 128. Is that really what you want?
I'm not going to spell out the solution because it's a basic logic issue. You should be able to figure it out on your own.
Just put yourself in the computer's place. If someone tells you they are younger than 64, then says they're older than 32... what would you mentally imagine the min/max to be?
EDIT: to rephrase..
If someone tells you they are older than 32 years old... what does that tell you about their maximum age?
If someone says they are older than 32 years old. That means min=32 +1 or 33.
However it doesn't say anything about thier max age. It should still be 128. I can't seem to figure it out.
#include <iostream>
#include <string>
usingnamespace std;
constint NUMBER_OF_GUESSES = 7;
int main()
{
int Guess;
int numGuesses = 1;
char answer;
int max = 128;
int min = 0;
cout << "This program will try to guess your age in under" <<endl;
cout << "7 attempts." << endl << endl;
cout << "Press \'o\' if the number is too high" <<endl;
cout << " or \'y\' if the number was too low, or \'x\'" << endl;
cout << " if the number was correct."
<< endl << endl;
Guess = max / 2;
while (numGuesses <= 7 && answer != 'x')
{
cout << "Are you " << Guess << " years old?" << endl;
cin >> answer;
if(answer =='o' || answer =='O')
{
min = Guess + 1;
Guess = (max + min)/2;
numGuesses ++;
}
elseif(answer == 'y' || answer == 'Y')
{
max = Guess - 1;
Guess = (max + min)/2;
numGuesses ++;
}
elseif(answer == 'x' ||answer == 'X')
{
cout << "Your age is: " << Guess
<< ". That was lucky!"
<< endl;
}
}
return 0;
}