Question:
pascal guessing game with number generator?
?
2010-03-24 10:28:03 UTC
Write a Pascal program that allows you to 'GUESS THE NUMBER'. Have the computer generate a random whole number between 1 and 100. Call this number SECRET.

Then, do the following steps :


Print out the word "GUESS", and input a guess from the keyboard
into a suitable variable name, say Guess.

WHILE Guess is not equal to SECRET do
BEGIN
Add 1 to the number of guesses (Use a suitable variable name)
Tell the user whether her guess is too high or too low.
Input the next Guess.
END;

Now, just output a message (like WOW! You got it!)
and tell the user how many guesses they took (that is, write out
the value of the variable you had used for this purpose).


Random number generation : Use the builtin function random to generate random numbers. Do

randomize;
secret := random(100) + 1;


early in your program to generate the secret number.
Four answers:
mystic smeg
2010-03-25 04:36:39 UTC
Hi,

:D

While I'm not trying to steel '?' glory, the answer [s]he has supplied won't compile. So I've re-written and tested a version (see below). - Good try '?'; I was the one to give you the thumbs up. :)



na



Program Tested;



var

Secret, Guess, Attempts: integer;



begin

Radomize;

Secret:=random(100)+1;



while (Guess <> Secret) do begin

write('Please guess a number: ');

readln(Guess);

if (Guess <> Secret) then begin

writeln('Wrong!');

Attempts := Attempts + 1;

end else begin

writeln('Correct!');

Guess := Secret;

end; {if}

end; {while}

end.
?
2010-03-24 10:45:16 UTC
its been a while...and my Pascal might be a little rusty...but it should be something like this:





var

Secret, Guess, Attempts: int

Secret:=random(100)+1



while (Guess <> Secret 10) do

begin

write('Please guess a number: ')

read(Guess);

if (Guess <> Secret)

then writeln('Wrong!')

Attempts := Attempts + 1;

else writeln('Correct!');

Guess := Secret

end;
cpcii
2010-03-24 10:33:22 UTC
Sounds like you got the algorithm down right, now code it, use your instruction book to find out how to do that.
anonymous
2010-03-24 10:37:49 UTC
So, do you have a question? If you're just asking someone to do your homework for you, you might as well be explicit and ask nicely.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...