In: Computer Science
ANSWER:-
GIVEN:-
// 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
PROGRAM SCREEN SHOT:-
OUTPUT:-