CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Homework 3 solution

{Author: Brenda Student
Section: CSCI 150-03
Date:   February 7, 2003
Assignment: Homework 3
Purpose: Play the high/low game}

program highlow;
var
	num:	integer;		{the number to guess}
	guess: integer;			{the current guess}
	counter: integer;		{count of number of tries}
begin
	randomize;		{This line "shuffles" the choices -- }
			        { otherwise they're the same every time.}
	num := random(21);    {This makes the choice. num will be between 0 and 20}
	writeln('I am thinking of a number between 0 and 20');
	guess := -1;		{num is never equal to -1}
	counter := 0;
	while guess <> num do
	begin
		writeln('Take a guess.');
		readln(guess);
		counter := counter + 1;
		if guess < num then
		begin
			writeln('Your guess is low.');
		end
		else if guess > num then
		begin
			writeln('Your guess is high.');
		end;
	end;
	writeln('Your guess is correct.  You made ', counter, ' guesses altogether.');
	writeln('Thanks for playing High/Low.');
end.