Thursday, July 30, 2009

Write this c++ program?

"Write a program that plays a guessing game where the computer tries to guess a number picked by the user. The program asks the user to think of a secret number, and then asks the user a sequence of guesses. After each guess, the user must report if it is too high or too low or correct. The program should count the guesses. (Hint: Maintain HighestPossible and LowestPossible variables, and always guess midway between the two. This is called a binary search.) Use only ifelse, while, do while coding, no fancy stuff and cout statements. The program output should look similar to:





Think of a number between 1 and 100 and press any key.


Is the number 50 (Correct, Low, High)? _h_


Is the number 25 (Correct, Low, High)? _h_


Is the number 13 (Correct, Low, High)? _l_


Is the number 19 (Correct, Low, High)? _c_


Number of guesses: 4


"





Underlined material is user input

Write this c++ program?
//Necessary Header Files


#include %26lt;iostream.h%26gt;


#include %26lt;ctype.h%26gt;





//Variable Declarations


int Guesses, HighestPossible, LowestPossible, MyGuess, Range;


char PlayAgain = 'Y', HighLowCorrect;





//Begin Program Code


int main()


{


//Initiate While Loop For Game


while (PlayAgain == 'Y')


{


//Initialize Variables For Game Play


HighestPossible = 100;


LowestPossible = 0;


Guesses = 0;





//Display User Instructions


cout %26lt;%26lt; "Please select a secret number between " %26lt;%26lt; HighestPossible


%26lt;%26lt; " and " %26lt;%26lt; LowestPossible %26lt;%26lt; "." %26lt;%26lt; endl;


cout %26lt;%26lt; "I will try to guess your number as quickly as possible. For each"


%26lt;%26lt; endl;


cout %26lt;%26lt; "guess please respond with either an H for too high, an L for too"


%26lt;%26lt; endl;


cout %26lt;%26lt; "low, or a C for correct." %26lt;%26lt; endl;





//Begin Guessing Loop


do


{


Range = HighestPossible - LowestPossible;


MyGuess = HighestPossible - int (Range/2);


cout %26lt;%26lt; "Is the number " %26lt;%26lt; MyGuess %26lt;%26lt; "(High, Low, Correct)? ";


cin %26gt;%26gt; HighLowCorrect;


HighLowCorrect = toupper(HighLowCorrect);


if (HighLowCorrect == 'H')


HighestPossible = MyGuess;


if (HighLowCorrect == 'L')


LowestPossible = MyGuess;


Guesses = Guesses + 1;


} while (HighLowCorrect != 'C'); // End Do-While Loop





//Display Guess Count And See If User Wants To Play Again


cout %26lt;%26lt; "It took me " %26lt;%26lt; Guesses %26lt;%26lt; " guesses." %26lt;%26lt; endl;


cout %26lt;%26lt; "Play Again? ";


cin %26gt;%26gt; PlayAgain;


PlayAgain = toupper(PlayAgain);


}; //End While


return 0;


} //End Program
Reply:Dude try to solve your assignments yourself first ....... Ask if something confuses or troubles you


No comments:

Post a Comment