Algorithms and Programming eXercises #7
Iteration using REPEAT...UNTIL
- Solutions
1.
Procedure TForm1.biggestclick(sender : TObject);
{accepts integers and outputs the biggest of them}
Var num,
biggest : integer;
Begin
biggest := 0; {a 'rediculously' small number}
repeat
num := strtoint(inputbox('Input','Enter a number (0 to quit)','0');
if num > biggest
then biggest := num
until num = 0;
showmessage('The biggest number inputted was ' + inttostr(biggest))
End;
2. Procedure TForm1.smallclick (Sender:TObject);
{accepts integers and outputs the smallest of them}
Var num,
smallest : integer;
Begin
smallest := maxint; {an 'unusually' large number}
repeat
num := strtoint(inputbox('Input','Enter a number (0 to quit)','0');
if (num < smallest) and (num <> 0)
then smallest := num
until num = 0;
showmessage('The smallest number inputted was ',smallest)
End;
3. Procedure TForm1.trapclick(sender:TObject);
{traps the user in a loop until they respond y/n}
Var reply : char;
temp : string;
Begin
repeat
temp := inputbox('Entry','Enter a Character','');
reply := temp[1];
until upcase(reply) in ['Y','N']
End.
4. Procedure TForm1.fibbonachiclick(Sender:TObject);
{generates and outputs fibbonacci numbers up until 1000}
Var num1, num2, temp : integer;
temp2 : string;
Begin
num1 := 1;
num2 := 1;
temp2 := '1 1 ';
repeat
temp := num1 + num2;
num1 := num2;
num2 := temp;
temp2 := temp2 + inttostr(num2) + ' ';
until num2 > 1000;
showmessage(temp2)
End;
5. Procedure TForm1.Gobbldeygookclick (Sender:TObject);
{Prints random letters and stops when a 'Z' is printed}
Var letter : char;
ascii : byte;
Begin
repeat
ascii := random(26)+ 65;
letter := char(ascii);
showmessage(letter)
until letter = 'Z'
End.
6. this is an exercise in changing values on forms when others change - see how you go,
there are lots of solutions
|