Вы находитесь на странице: 1из 8

Without using a computer, hand trace each the following code snippets and

write the output if there is no error:


a. for i := 0 to 10 do
write(' * ');
**********
b. for i := -1 to 1 do
begin
write(' * '); writeln(' + ');
end;

c. for i := 'z' to 'x' do


write(i);
no error but will not be executed
d. for i := 'E' to 'H' do
write(i);
EFGH
e. for i := 'e' downto 'a' do
write(i);
EDCBA
f. for i := 1.1 downto 2.0 do
write(i);
no error but will not be executed
g. for i := false downto true do
write(i);
no error but will not be executed
h. for j := 1 to 10 do
begin
writeln(j);
j := j + 3;
end;
1
5
9

i. for i := 10 downto 0 do
begin
writeln(i);
i := i - 2;
end;
10
7
4
1
Now, try each of the code snippets with the compiler. Make sure to declare the
variables with suitable data types. Compare the output from the program run
with the one that you have written on the paper. Discuss!
Activity #2
a. By applying for..do statement, write a Pascal program that prints a
sequence of numbers in descending order from 100 to 1 (inclusive).
b. Modify your program in part (a) to make it prints even numbers only.
program dua;
var i : integer;
begin
for i := 100 downto 1 do
begin
if i mod 2 = 0 then
writeln(i);
end;
readln
end.
program dua;
var i : integer;
begin
for i := 100 downto 1 do
begin
if i mod 2 = 0 then
write(i:8);
end;
readln
end.

c. Write a Pascal program that receives a positive integer (N) and prints a series
of numbers from N to 1 (inclusive) in a descending order.

program dua;
var N,i : integer;
begin
write('please enter your N: ');
readln (N);
for i:=N downto 1 do
writeln (i);
readln
end.
d. Write a Pascal program that displays a series of alphabets in ascending order
from A to Z.
program dua;
var i : char;
begin
for i:= 'A' to 'Z' do
write (i:2);
readln
end.
e. Modify your program in part (d) so that the program will display the letter at
the indices dividable by 3.
program dua;
var i : char;
begin
for i:= 'A' to 'Z' do
if (ord(i)mod 3=0)then
write (i:2);
readln
end.
f. Write a Pascal program that receives start value and end value. Then, your
program should display all even numbers that are dividable by 3 from the
start value to the end value inclusive. Also, you should display the sum of the
numbers at the end of the program run. Give a proper message should the
end value is less than the start value.
program dua;
var val1,val2,i,sum : integer;
begin
writeln('please input your start value');
readln(val1);
writeln('please input your end value');
readln(val2);
if (val1<val2)then

begin
sum :=0;
for i:=val1 to val2 do
if (i mod 2 = 0) and (i mod 3 = 0) then
sum := sum + 1;
write(i:5);
writeln(sum);
readln;
end
else
writeln('your numbers are incorrect');
readln
end.
Activity #3
Write a Pascal program that asks a user to enter n real numbers. By applying
for..do loop, your program should find and display the largest, lowest, sum
and average of the numbers. Give a proper message for invalid user input.
Sample program run #1
How many numbers do you have? > -3
Sorry, you have entered an invalid input.
Thank you, come and play again later. Cheers.
Sample program run #2
How many numbers do you have? > 0
Opps, you don't have any number for me to process.
Thank you, come and play again later. Cheers.
Page 2 of 5
FAT0015 Fundamental of Programming I Week 9 Lab 1
Sample program run #3
How many numbers do you have? > 6
Please enter a number_1 --> 7.5
Please enter a number_2 --> -2.2
Please enter a number_3 --> 0
Please enter a number_4 --> 3
Please enter a number_5 --> 5.5

Please enter a number_6 --> 1.9


The lowest number is -2.2
The highest number is 7.5
The sum is 15.7
The Average is 2.6
Thank you, come and play again later. Cheers.
Activity #4
Without using a computer, hand trace each the following code
snippets/programs and write the output if there is no error:
a. i := 1;
while i <= 10 do
begin
writeln(*:i);
i := i + 1;
end;
writeln(i - 1, ' stars.');
b. i := 10;
repeat
writeln(' * ':i);
i := i - 1;
until i = 1;
writeln('How many stars?');
c. i := 1; j := 4; k := 5;
while j < (i * k) do
begin
write(i,'#');
write(j,'#');
writeln(k,'#');
j := j * 2;
i := i + 1;
end;
writeln(i, j, k);

d. i := 1; j := 4; k := 5;
repeat
write(i,'#'); write(j,'#');
writeln(k,'#');
j := j * 2;i := i + 1;
until j < (i * k);
writeln(i, j, k);
Page 3 of 5
FAT0015 Fundamental of Programming I Week 9 Lab 1
e. i := 1; j := 4; k := 5;
repeat
write(i,'#');
write(j,'#');
writeln(k,'#');
j := j * 2;
i := i + 1;
until NOT (j < (i * k));
writeln(i, j, k);
f. i := 10;
while i >= 0 do
write(i);
i := i - 1;
g. program trace;
var
i: integer;
begin
i := 1;
while i <= 10 do
begin
write(i:i);
i := i + 2;
end;

readln();
end.
h. program trace;
const SENTINEL = 9;
var
i: integer;
continue: boolean;
begin
continue:= true;
i := 0;
repeat
writeln(i:i);
i := i + 2;
continue := i < SENTINEL;
until not continue;
readln();
end.
Now, try each of the code snippets/programs with the compiler. Make sure you
declare all the variables with suitable data types respectively. Compare the
output from the program run with the one that you have written on the paper.
Discuss!
Page 4 of 5
FAT0015 Fundamental of Programming I Week 9 Lab 1
Activity #5
a. Using while..do loop, write a Pascal program that prints a sequence of
numbers in descending order from 100 to 1 (inclusive).
b. Rewrite the program in part (a) using repeat..until.
c. Using while..do, write a Pascal program that receives a positive integer
n from the user, and prints a series of numbers from n to 1 (inclusive) in a
descending order.
d. Using repeat..until loop, write a Pascal program that prints a series of
alphabets in a descending order from Z to A.

e. Rewrite the program in part (d) using while..do loop, and print only the
letters at the index dividable by 3.
f. Write a Pascal program that asks the user to enter n integers, where n
should be entered by the user, then the program should display how many
positive integers and negative integers are entered. Write the program using
repeat..until statement with zero (0) as the sentinel value.
Activity #6
a. Using while.. do loop, write a Pascal program that converts meters to
feet. The program should display meters from 1 to 10 and display the
corresponding feet equivalents. Use the relationship that 1 meter is
equivalent to 3.28 feet.
b. Rewrite the program in part (a) using repeat..until loop. Make the
program more dynamic where it will receive the starting value, stop value
and the increment value from the user. Then, the program should display the
table accordingly. Give proper messages for invalid user inputs.

Вам также может понравиться