Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Explain virtual class?

1021


What is the maximum value of a unsigned char a) 255 b) 256 c) 128

1062


Explain the static storage classes in c++.

1256


Can you please explain the difference between using macro and inline functions?

1034


How would you implement a substr() function that extracts a sub string from a given string?

1031


What is the main use of c++?

1115


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

1059


Are iterators pointers?

1135


What is a rooted hierarchy?

1145


What is the difference between a pointer and a link in c ++?

1044


Why c++ is better than c language?

1008


How one would use switch in a program?

1063


What is oop in c++?

1066


How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?

2303


What is the difference between struct and class?

1528