In: Computer Science
Task 2C: User picks an integer in a range and computer guesses by randomly This approach is similar to Task 2B. The only difference is, the computer will generate a random integer in the range as a guess. In this strategy, a guess is a random int in a given range. Hence, for the same range and same answer, the number of guesses will be different. For example, suppose the range is in [0, 10] and the answer is 7.
These are possible outputs.
The following running takes 8 tries to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10].
Computer will guess. guess #1: 10. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #2: 1. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #3: 9. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #4: 2. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #5: 4. How is my guess? 1. too big 2. too small 3. just right
guess #6: 8. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #7: 5. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #8: 7. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 3 Congratulations! The answer is 7.
In the following running, it takes only three guesses to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10].
Computer will guess. guess #1: 6. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #2: 8. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #3: The answer must be 7.
NOTE- Must be done with C++
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
// including required headers
#include <iostream>
using namespace std;
int main()
{
// variables to store left and right boundaries
int left,right;
// prompt for left end range
cout << "Enter left end in range: ";
// reading value left
cin >> left;
// prompt for right end range
cout << "Enter right end in range: ";
// reading value right
cin >> right;
// Printing the user input left and right
cout << "User has an int in ["<<left<<",
"<<right<<"].\n";
// Printing Computer Guesses
cout << "Computer will guess.\n";
// variables to store count of Guesses ,Option and Computer
guesses
int Guesses = 0,Option = 0, Computer_guess = 0;
// A infinite while loop
while(true)
{
// incrementng Guesses count
Guesses ++;
// generating a random number in range of left and right
Computer_guess = (rand()%(right - left + 1)) + left;
// Printing Computer guess
cout << "guess #" << Guesses <<": " <<
Computer_guess << "\n" ;
// Printing options
cout << "How is my guess? 1. too big 2. too small 3. just
right\n";
// prompt for option entry
cout << "Enter only 1, 2, or 3: ";
// reading Option from user
cin >> Option;
// If User Option is 1
if(Option == 1)
{
// As the generated value is too big
// change right value
right = Computer_guess - 1;
}
// If User Option is 2
else if(Option == 2)
{
// As the generated value is too small
// change left value
left = Computer_guess + 1;
}
// If User Option is 3
else if(Option == 3)
{
// Printing the Congratulation Message
cout << "Congratulations! The answer is " <<
Computer_guess <<".";
// breaking the loop
break;
}
}
return 0;
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!