Write a C++ program that asks the user to choose a number
between 1 and 1000. Then,
your program should be able to guess the number by asking
the user no more than 10 yes/no
questions. Use a while loop in your program



Write a C++ program that asks the user to choose a number between 1 and 1000. Then, your program s..

Answer / i4o

Instead of giving the full program the expectation of this question could be logical skills or applying heuristics in deducing the given problem domain (1-1000) in to smaller pieces. The decision tree could be like whether the number could be even or odd thus eliminating 50% then based on number of digits etc. But it expects to use while loop. So it should be solved mathematically. The following snippet (C#) uses the something similar to binary search (Cutting the problem domain exactly by half each time) and any number could be cracked with 10 questions.

static void Main(string[] args)
{
int low = 1, high = 1000;
int mean;
string userresponse;

while (low != high)
{
mean = (low + high) / 2;

Console.WriteLine("Is the number between {0} & {1}", low, mean);
userresponse = Console.ReadLine();
if (userresponse.CompareTo("y") == 0)
{
high = mean;
}
else
{
low = mean+1;
}
}

Console.Write("You Guessed : {0}", low);
Console.Read();
}

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More C++ General Interview Questions

What is the difference between new() and malloc()?

0 Answers  


What is the first name of c++?

0 Answers  


Are vectors passed by reference c++?

0 Answers  


what is difference between internet and Internet?

12 Answers   College School Exams Tests, Microsoft, MIT, TCS,


How const functions will be treated by compiler?

3 Answers   Symphony,






given the code segment below void main() { cout<<"my no. is"; } question is how can we get the output hai aravind my no. is 99999999999 without editig the main().

2 Answers  


What are c++ stream classes?

0 Answers  


What is a class definition?

0 Answers  


What is Destructor in C++?

0 Answers  


Write about a nested class and mention its use?

0 Answers  


What is a set in c++?

0 Answers  


What are containers in c++?

0 Answers  


Categories