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

Which programming language's unsatisfactory performance led to the discovery of c++?

0 Answers  


wrong statement about c++ a)code removably b)encapsulation of data and code c)program easy maintenance d)program runs faster

11 Answers  


Can you pass an array to a function in c++?

0 Answers  


What is the difference between operator new and the new operator?

3 Answers   Amazon, TCS, Wipro,


What is function prototyping?

0 Answers  






How many lines of code you have written for a single program?

4 Answers   BoA,


how many trys can we write in one class

3 Answers   Cap Gemini,


class professor {}; class teacher : public virtual professor {}; class researcher : public virtual professor {}; class myprofessor : public teacher, public researcher {}; Referring to the sample code above, if an object of class "myprofessor" were created, how many instances of professor will it contain? a) 0 b) 1 c) 2 d) 3 e) 4

4 Answers   Quark,


Is there a c++ certification?

0 Answers  


Why c++ is called oop?

0 Answers  


What is token c++?

0 Answers  


Is rust better than c++?

0 Answers  


Categories