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 are the techniques you use for debugging?

1 Answers   Adtran,


Explain the concept of copy constructor?

0 Answers  


What is the Diffrence between a "assignment operator" and a "copy constructor"?

3 Answers   Wipro,


What is an inline function in c++?

0 Answers  


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

0 Answers  






Can union be self referenced?

0 Answers  


What is copy constructor? Can we make copy constructor private in c++?

0 Answers  


What is the difference between public, private, and protected access?

0 Answers  


If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2.

1 Answers  


What is the best sorting algorithm, when there is a large amount of data, that cannot be fit in the main memory. ?

1 Answers   Yahoo,


How to detect memory leaks in c++

1 Answers   Mphasis,


What do you mean by volatile and mutable keywords used in c++?

0 Answers  


Categories