Answer to Question #289973 in C++ for ali

Question #289973

Create a program that asks the user a series of questions that begin, “Is your number bigger than…” in order to deduce what integer between 1 and 100 they are thinking of. The user should only be able to answer ‘y’ or ‘n’ to each question. (Hint: each time round the loop, you should be seeking to halve the range in which number could lie, so store an upper bound and a lower bound, and change one or the other according to the user’s answers).


1
Expert's answer
2022-01-23T09:40:46-0500
#include <iostream>
 

int main()
{
    int number = 0;
    char answer = 'n';
    int lower_border = 0;
    int high_border = 0;
    std::cout << "Imagine a number between 0 and 10000" << std::endl;
    std::cin >> number;
    while(true)
    {
        int middle = (lower_border + high_border) / 2 ;
        std::cout<<"Is your number bigger than middle?"<<std::endl;
        std::cin>>answer;
        if(answer == 'y')
        {
            lower_border = middle;
        }
        else
        {
            high_border = middle;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog