First off, I did do a search but came up with nothing that specifically helped me. I'm doing a problem where the user inputs a number and the computer has to guess it based on the user saying higher or lower. There's more to the program, but that doesn't matter.
Is it possible to make the computer generate a value using binary search between the max number and min number?
For example, I enter 500 and the minimum is 1. The computer's first guess is obviously 250 (or number/2). If I say higher, the computer will now have a minimum value of 250. So the next guess would be between 250 and 500, and would be 375 ( 250 / 2 + 250).
When I run the program, it assigns the min and max numbers accordingly, but never (or rarely) produces a value in between the two values, which is always should.
Here's a portion of my code for the number being higher:
I think the statement "number= rand() % n + 1;"
is wrong. Can anyone lead me in the right direction? I realize this should be easy and I'm probably making it harder than it really is! Thanks for any help that can be given :)
if (response == 'h')
number = rand() % n + 1;
cout << "Next guess: " << number << ". Is your number higher or lower? " ;
cin >> response;
min = number; // set min number to last guess by computer