Question

In: Computer Science

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 any integer that is bigger than guess. When the feedback is 2 (too small), the computer discards the integers any integers that is smaller than the guess. When computer makes a correct guess, the game ends.

Note that your code must ensure a user to entre a feedback in [1, 3].

Sample input/output:

Enter left end in range: 0

Enter right end in range: 10

User has an int in [0, 10]. Computer will guess.

guess #1: 5. 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: 6. How is my guess?

1. too big   2. too small   3. just right

Enter only 1, 2, or 3: 2

guess #4: The answer must be 7.

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()

{

    int left, right;

    // read left

    cout << "Enter left end in range: ";

    cin >> left;

    // read right

    cout << "Enter right end in range: ";

    cin >> right;

    // print

    cout << "User has an int in [" << left << ", " << right << "]. Computer will guess.\n";

    int i = 1, guess, feedback;

    // loop till guess is correct

    while (true)

    {

        guess = (left + right) / 2;

        // print guess

        cout << "guess #" << i << ": " << guess << ". How is my guess?\n";

        cout << "1. too big  2. too small  3. just right\n";

        cout << "Enter only 1, 2, or 3: ";

        cin >> feedback;

        // if too big

        if (feedback == 1)

        {

            if (guess == left + 1)

            {

                cout << "guess #" << i + 1 << ": The answer must be " << left << ".\n";

                break;

            }

            right = guess - 1;

        }

        // if too small

        else if (feedback == 2)

        {

            if (guess == right - 1)

            {

                cout << "guess #" << i + 1 << ": The answer must be " << right << ".\n";

                break;

            }

            left = guess + 1;

        }

        // else if guess is correct

        else

        {

            break;

        }

        i++;

    }

    return 0;

}

.

Output:

.


Related Solutions

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....
(Please answer in C++ ) This approach is similar to Task 2B. The only difference is,...
(Please answer in C++ ) 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...
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
Consider the reaction A+2B⇌CA+2B⇌C whose rate at 25 ∘C∘C was measured using three different sets of...
Consider the reaction A+2B⇌CA+2B⇌C whose rate at 25 ∘C∘C was measured using three different sets of initial concentrations as listed in the following table: Trial [A][A] (MM) [B][B] (MM) Rate (M/sM/s) 1 0.50 0.050 1.5×10−2 2 0.50 0.100 3.0×10−2 3 1.00 0.050 6.0×10−2 Part A What is the rate law for this reaction? Express the rate law symbolically in terms of kkk, [A][A], and [B][B]. View Available Hint(s)
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...
Code in C ++ that asks the user for an integer and that prints whether or...
Code in C ++ that asks the user for an integer and that prints whether or not the number is divisible by three in the integers. If the number is not divisible by three, the program must write a message indicating what the remainder is obtained by dividing the number by three.
Using the data, determine the rate constant of this reaction. A+2B ---> C+D
Using the data, determine the rate constant of this reaction.A+2B ---> C+Dtrial     [A] (M)    [B] (M)   Rate (M/s)1         0.290       0.260     0.02012         0.290      0.520      0.02013         0.580     0.260       0.0804Number K=???Units= ?? M.s-1, M-2s-1, s-1,M-1s-1
Using the given data, determine the rate constant of this reaction. A + 2B --> C...
Using the given data, determine the rate constant of this reaction. A + 2B --> C + D Given Data: Trial [A](M) [B](M) RATE (M/s) 1 .230 .330 0.0231 2 .230 .660 0.0231 3 .460 .330 0.0924 k=? the units are M-1 s-1
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT