Question

In: Computer Science

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 any integers that is smaller than the guess. When computer makes a correct guess, the game ends. In C++

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

C++ code screenshot:

C++ code:

#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int left, right, guessNumber = 1, mid, choice;

    cout << "Enter left end in range: ";
    cin >> left;
    cout << "Enter right end in range: ";
    cin >> right;
    cout << "User has an int in [" << left << ", " << right << "]. Computer will guess.\n";

    while (left < right)
    {
        mid = (left + right) / 2;
        cout << "guess #" << guessNumber << ": " << mid << ". How is my guess?\n";
        cout << "1. too big   2. too small   3. just right \n";
        cout << "Enter only 1, 2, or 3: ";
        cin >> choice;
        while (cin.fail() || choice < 1 || choice > 3)
        {
            //something went wrong, we reset the buffer's state
            cin.clear();
            //and empty it
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            cout << "\nINVALID INPUT !!! \n";
            cout << "Enter only 1, 2, or 3: ";
            cin >> choice;
        }
        if (choice == 3)
        {
            cout << "\n\nThank You for playing...\n";
            return 0;
        }
        else if (choice == 1)
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }
        guessNumber++;
    }
    cout << "guess #" << guessNumber << ": The answer must be " << left << ".\n";
    return 0;
}

Output 1:

Output 2:


Related Solutions

Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Checker for integer string Forms often allow a user to enter an integer. Write a program...
Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
Assume user will enter letters or numbers that are out of range. When user inputs invalid...
Assume user will enter letters or numbers that are out of range. When user inputs invalid values, show an alert message and ask user to enter a valid value again. Validate first and then proceed with the program. isNaN() method may be useful. Must validate user input. 1. Suggested Filename: fortune.html & fortune.js Write a program that simulates a fortune cookie. The program should display one of five unique fortunes, depending on user input. The user must enter a number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT