this assignment, you are to write aprogram that guesses what you have in your mi
ID: 3819067 • Letter: T
Question
this assignment, you are to write aprogram that guesses what you have in your mind. No, computers can't read your mind, but at least guess a number you have in mind given that you tell the program whether the number the program guessed is higher or lower than your number. Since humans are much more creative than the computer applications, we will level the playing field by limiting the number we select to be in between 1 and 100. The program should work as following 1. You will pick a number between 1 and 100. This number should never be used in your program 2. The program should pick a random number in between 1 and 100 3. The program asks user whether the number is comect, higher, or lower. 4. If it is the correct guess, print "Correct guess" and exit out of the program. 5. If not correct, answer"h", or "I" to indicate the guess is either higher or lower than the correct number. 6. If lower, then the next number should be higher than the previous guess. 7. If higher, then the next number should be lower than the previous guess. Repeat steps 3 through 7 until the comect guess is made. 9. The program should keep track of the number guesses made until the correct 10. Capture screen outputsofall guesses and responses along with the final number taken, guesses Submit your source code and output screen capture.Explanation / Answer
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
int getRandomInRange(int lower, int higher)
{
return rand()%(higher-lower + 1) + lower;
}
int main()
{
srand (time(NULL));
int n;
int low = 1;
int high = 100;
int guess;
int guessCount = 0;
char input;
while(true)
{
guess = getRandomInRange(low, high);
guessCount++;
cout << "My guess is: " << guess << endl;
cout << "Enter guessed number is: c correct h higher l lower ";
cin >> input;
if (input == 'c')
{
cout << "Correct guess. " << guessCount << " guesses."<< endl;
break;
}
else
{
if (input == 'h')
{
high = guess;
}
else
{
low = guess;
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.