I’m going through Bjarne Stroustrup’s “Programming Principles and Practice Using C++”. I am on chapter 4, exercise 4.
The excercise is as follows:
Write a program to play a numbers guessing game. The user thinks of a number between 1 and 100 and your program asks questions to figure out what the number is (e.g. “Is the number you are thinking of less than 50?”). Your program should be able to identify the number after asking no more than seven questions. Hint: Use the < and <= operators and if-else constructs.
Now that is great, and I’ve managed to implement that fine.
I’d thought I’d try and push myself and try and implement this using a loop and adjusting lower or upper bounds each time.
Here is my code:
#include "std_lib_facilities.h"
int main( ){
int count = 0;
int lowerBound = 0;
int upperBound = 100;
string userInput = "";
while ( lowerBound != upperBound ){
// Increment count
++count;
int halfRange = 0;
// Make halfRange a while number, round up if any decimal portion.
double range = ( upperBound - lowerBound ) / 2;
int rangeDelta = range - (int)range;
if ( rangeDelta != 0 )
halfRange = (int)range + 1;
else
halfRange = range;
cout << count <<": Is your number between " << lowerBound << " and " << lowerBound + halfRange << "? ";
cin >> userInput;
// Reset the bounds
if ( userInput == "y" || userInput == "Y" )
upperBound -= halfRange;
else if ( userInput == "n" || userInput == "n" )
lowerBound += halfRange;
else {
--count;
cout << "Error! Answer could not be understood.";
}
}
cout << "lowerBound: " << lowerBound << ", upperBound: " << upperBound << "\n\n";
cout << "Your number is: " << lowerBound << "\n";
return 0;
}
The problem? Well, it occurs when it gets to numbers where there is a decimal portion and using integer division, which throws the decimal part away. If you use the number 48, the program guesses to 47 and 47.
Any clues to get me going? I think I’m quite close, but will appreciate some help.
Thanks,
Matt
I think that there is still an issue in your code (even if the bugs signaled before are fixed): it should be clear that upperbound and lowerbound are both included in possible numbers remaining and this is not clear from your code: at the first question, if I answer ‘y’, the new interval is [0,50] and if I answer ‘n’, the new interval is [50,100], which is wrong! 50 should not be included in the second interval.
To fix this, you should change the update of your bounds to something like:
Now there is still a problem with the last question, which remains the same forever. The problem is that when range=1, you have halfrange=1 as well and the question keeps the same.
To solve that issue, you should round down range. Just define it like:
and now your code should work. Here is the code I would use for the while loop:
}
Remark that now we only use integers, no double at all! If you use integers, try to avoid passing by double, and use rather integer division and modulo to work with them (and do not use double to represent integers).
Hope it helps!