Hello! This is my solution to a problem in a book I'm reading. The question is as follows:
"2. Write a program that picks a number between 1 and 100, and then lets the user guess what the
number is. The program should tell the user if their guess is too high, too low, or just right.
3. Write a program that solves the guessing game from problem 2. How many guesses does your program need?"
So the following is really an answer to number 3.
Specifically, I would like any ideas on how to make it shorter, neater, more elegant, etc.
# include <ctime>
# include <cstdlib>
# include <iostream>
usingnamespace std;
//Random number function
int randRange (int low, int high)
{
return rand() % (high - low + 1) + low;
}
int main()
{
srand(time(NULL));
//Random number is generated between 1 and 100
int number=randRange(1,100);
//First guess is always 50
int guessNumber=50;
//Guess attempts are counted from zero
int guessAttempts=0;
cout << "Guess what number I'm thinking of...it's between 1 and 100 (inclusive)." << '\n';
cout << guessNumber << '\n';
guessAttempts++;
// While the random number does not equal the guess number, this loop runs
while (number != guessNumber)
{
if (number > guessNumber)
{
// This number holds the number just before the number is too high
// so that it averages out in the next condition
int previousGuessNumber = guessNumber;
// Add 25 until too high
cout << "Too low..." << '\n';
guessNumber = guessNumber + 25;
cout << guessNumber << '\n';
guessAttempts++;
if (number < guessNumber)
{
// While loops prevent the number from 'falling through'
// prematurely and resetting 'thought process'
while (number < guessNumber)
{
// Again, stores number for use in averaging in next condition
int previousGuessNumber2 = guessNumber;
cout << "Too high..." << '\n';
guessNumber = (guessNumber + previousGuessNumber) / 2;
cout << guessNumber << '\n';
guessAttempts++;
if (number > guessNumber)
{
// Repeat same general process until the algorithm is air-tight!
while (number > guessNumber)
{
int previousGuessNumber3 = guessNumber;
cout << "Too low..." << '\n';
guessNumber = (guessNumber + previousGuessNumber2) / 2;
cout << guessNumber << '\n';
guessAttempts++;
if (number < guessNumber)
while (number < guessNumber)
{
previousGuessNumber2 = guessNumber;
cout << "Too high..." << '\n';
guessNumber = (guessNumber + previousGuessNumber3) / 2;
cout << guessNumber << '\n';
guessAttempts++;
}
}
}
}
}
}
// When guess is too high
// same general process, but number is divided by two until
// 'averaging conditions' are met
elseif (number < guessNumber)
{
int previousGuessNumber = guessNumber;
cout << "Too high..." << '\n';
guessNumber = guessNumber / 2;
cout << guessNumber << '\n';
guessAttempts++;
if (number > guessNumber)
{
while (number > guessNumber)
{
int previousGuessNumber2 = guessNumber;
cout << "Too low..." << '\n';
guessNumber = (guessNumber + previousGuessNumber) / 2;
cout << guessNumber << '\n';
guessAttempts++;
if (number < guessNumber)
{
while (number < guessNumber)
{
int previousGuessNumber3 = guessNumber;
cout << "Too high..." << '\n';
guessNumber = (guessNumber + previousGuessNumber2);
cout << guessNumber << '\n';
guessAttempts++;//A bunch of while statements to keep the value from
// falling through
if (number > guessNumber)
while (number > guessNumber)
{
cout << "Too low..." << '\n';
guessNumber = (guessNumber + previousGuessNumber3) / 2;
cout << guessNumber << '\n';
guessAttempts++;
}
}
}
}
}
}
}
cout << "Correct!" << '\n';
cout << "Number should be: " << number << '\n';
cout << "# of guesses: " << guessAttempts << '\n';
}
Your answer to the second problem I find to be too tedious for the question itself, but it all makes sense though and I appreciate the effort you put in but I do not think this is the response the book was looking for ;)
Your answer to the second problem I find to be too tedious for the question itself, but it all makes sense though and I appreciate the effort you put in but I do not think this is the response the book was looking for ;)
He didn't supply an answer to the second problem here, so I'm not sure what you're going on about. Certainly his solution to the third problem, while not ideal, will take fewer "guesses" on average than yours.
@OP: Your algorithm is basically (a typical binary search:)
For a given range, split the range to determine what the guess should be.
If the guess is too high, reduce the upper limit of the range to one below the guess.
If the guess is too low, increase the lower limit of the range to one above the guess.
Repeat until the number is found.
Which might look something like the following in a more compact form:
Hey prince, just put the code in between (code) and (/code), but replace the parentheses with brackets.
Cire, I love that solution. Do you think you could tell me how you came to that? It's very clean and simple. However, I don't understand what the point of adding/subtracting 1 from guess is. Could you explain?
I don't understand what the point of adding/subtracting 1 from guess is. Could you explain?
Since we know the guess is wrong, there is no reason to include the value of the guess in the next iteration.
e.g. if the guess is 75 and that is too big, then maxguess for the next iteration becomes 74. Likewise if the guess is 25 and that is too small, then minguess for the next iteration becomes 26.