0

Hi, i am writing a program that plays “guess what number I’m thinking of…” with you. User Inputs guesses and output whether the number is too low, too high, or right-on. I already wrote the program to have the user guess what number the computer is guessing; however, can anyone please help me on writing an algorithm to have the computer guess the user's number?

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Moschops
0

How would you guess the number? Write out those steps, and that's your algorithm.

It might be something along the lines of:

Pick a number halfway between the minimum and maximum possible numbers.
If correct, stop.
If too high, change possible maximum to this guess minus 1 and start again.
If too low, change possible minimum to this guess plus 1 and start again.

0

no i am asking how to i have the computer guess what number the user is thinking!

0

Moschops is saying you program the computer to do the same thing you do without the computer. What's your first guess? Make the computer to the same thing.

0

no i am asking how to i have the computer guess what number the user is thinking!

I just told you how. Perhaps some code might help.

int minimumNumber = 0;
int maximumNumber = 100;
int guessCorrect = 0;
int nextGuess;
cout << "Think of a number between 0 and 100." << endl;


while(!guessCorrect)
{
  nextGuess = getNextguess(minimumNumber, maximumNumber);
  cout << "Is it " << nextGuess << "?" << endl;
  // Get answer from user here - if yes, change guessCorrect to 1
  //    if they say higher, change the minimumNumber
  //    if they say lower, change the maximumumber
}

return 0;

}

getNextguess is a function that makes a sensible guess between the two numbers passed to it.

It seems that you've realised the actual difficult aspect of programming. Syntax is easy; the hard part is working out how to actually solve the problem.

0

Oh yea thanks very much i understand now. But i believe "getNextguess" is undefined in your syntax. does it need to be declared before the cout? But then if i do that wouldn't i be using it as a function?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.