Question

In: Computer Science

Task 2C: User picks an integer in a range and computer guesses by randomly This approach...

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++

Solutions

Expert Solution

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!


Related Solutions

In C++ Task 2B: User picks an integer in a range and computer guesses using approach...
In C++ Task 2B: User picks an integer in a range and computer guesses using approach similar to binary-search Enter the left end and right end of a range of integers. User chooses an integer in that range. Computer makes a guess that equals the mid of the range. User gives a feedback for each guess: 1 for too big, 2 for too small and 3 for just right. When the feedback is 1 (too big), the computer throws away...
Combinatorics: 6. A mathematician picks and integer i from the set {1,2,3,...,15} and a computer scientist...
Combinatorics: 6. A mathematician picks and integer i from the set {1,2,3,...,15} and a computer scientist tries to find the number by asking questions of the form: Is i<x, i>x, or i=x? Show that the number can always be found using three questions
Enter the left end and right end of a range of integers. User chooses an integer...
Enter the left end and right end of a range of integers. User chooses an integer in that range. Computer makes a guess that equals the mid of the range. User gives a feedback for each guess: 1 for too big, 2 for too small and 3 for just right. When the feedback is 1 (too big), the computer throws away any integer that is bigger than guess. When the feedback is 2 (too small), the computer discards the integers...
Discuss mental models (user task environment, human-computer interaction, environmental issues) that may be influencing the human...
Discuss mental models (user task environment, human-computer interaction, environmental issues) that may be influencing the human component of the e-prescription process in hospitals or clinics. And what is the reason for poor adoption of health information technology tools such as poor usability, variability in goals, identified care gap, lack of information at the right time, lack of care coordination, creation of workarounds in e-prescribing process?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT