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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Is it legal in c++ to overload operator++ so that it decrements a value in your class?

616


Is java easier than c++?

595


What does it mean to declare a destructor as static?

629


What is difference between n and endl in c++?

586


What is new in c++?

582






Must accepts "Maestro Cards" Tax for bike should be less than 15 Total number of lanes is more than 10 Must provides monthly pass Write a method: boolean isGoodTollBridge(String[] cardsAccepted, String[] tollTax, boolean hasMonthlyPass, int numberOfLanes); String[] cardsAccepted A String array of names of card types accepted for payment of toll tax, it can be null if the toll does not accept any card String[] tollTax A String array of toll tax chart (say “Train : 300”,”BullCart : 10”) boolean hasMonthlyPass This parameter defines whether there is any monthly pass available or not int numberOfLanes This parameter defines the number of lanes for each side

3047


Why do we use classes in c++?

583


What is the this pointer?

639


What's c++ used for?

602


What is c++ namespace?

712


What does the nocreate and noreplace flag ensure when they are used for opening a file?

685


When do we run a shell in the unix system? How will you tell which shell you are running?

558


What's the "software peter principle”?

627


Define anonymous class.

627


What are the general quetions are in DEna bank manager IT/System interviews?

1540