Algorithms and Programming eXercises #9
Iteration using FOR..DO - Solutions
1.
(a) Var thing : char;
temp : string;
Begin
temp := '';
for thing := 'a' to 'z' do
temp := temp + thing;
showmessage(temp);
End;
(b) Var thing : char;
temp : string;
Begin
temp := '';
for thing := 'z' downto 'a' do
temp := temp + thing;
showmessage(temp)
End;
(c) Var counter : byte;
temp : string;
Begin
temp := '';
for counter := 1 10 15 do
temp := temp + inttostr(counter) + ' ';
showmessage(temp)
End;
(d) Var counter: byte;
temp : string;
Begin
temp := '';
for counter := 1 10 15 do
temp := temp + inttostr(counter) + ' ';
showmessage(temp);
End;
(e) Var loopything : byte;
temp : string;
Begin
temp := '';
for loopything := 1 to 15 do
temp := temp + inttostr(loopything*loopything) + ' ';
showmessage(temp)
End;
(f) Var thing:integer;
loop : byte;
temp : string;
Begin
thing := 1024;
temp := '';
for loop := 1 to 11 do
begin
temp := temp + thing + ' ';
thing := thing div 2
end;
showmessage(temp)
End;
2. {adds all numbers from 1 to 100}
Var total : integer;
loop : byte;
Begin
total := 0;
for loop := 1 to 100 do
total := total + loop
showmessage('The total of all numbers from 1 to 100 is '+ inttostr(total))
End;
3. {adds all squares of numbers from 1 to 10}
Var total : integer;
loop : byte;
Begin
total := 0;
for loop := 1 to 10 do
total := total + loop*loop
showmessage('The total of all the squares of numbers from 1 to 10 is '+inttostr(total))
End;
4. experiment with a memo box ... ask your teacher to explain their use...
|