You are on page 1of 11

Chapter 9 Formulation of Algorithms Question Bank

Chapter 9 Formulation of Algorithms


Conventional Questions

(U03C09L02Q001)
Write Pascal statements to perform the following jobs.
(a) Display the words ‘Pascal Programming!’ without the quotation marks.
(b) Prompt a user to input his/her name and then display his/her name two times in a line.
(c) Prompt a user to input a number and then display the square of the number.
(7 marks)
Answers
(a) write(‘Pascal Programming!’)
(b) write(‘Input your name: ’);
readln(Name);
write(Name);
writeln(Name)
(c) write(‘Input a number: ’);
readln(Num);
writeln(Num * Num)

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(U03C09L02Q002)
Write a program List that receives the age, weight and height of 3 students and then displays
them in table format. A sample output is given below.

Sample output:
Enter the age, weight and height of student 1: 15 60.5 169.3
Enter the age, weight and height of student 2: 16 48 160.5
Enter the age, weight and height of student 3: 15 51.3 157

Age Weight Height


Student 1 15 60.5 169.3
Student 2 16 48.0 160.5
Student 3 15 51.3 157.0
(5 marks)
Answer
program List;
var
Age1, Age2, Age3 : integer;
Weight1, Weight2, Weight3 : real;
Height1, Height2, Height3 : real;
begin
write(‘Enter the age, weight and height of student 1: ’);
readln(Age1, Weight1, Height1);
write(‘Enter the age, weight and height of student 2: ’);
readln(Age2, Weight2, Height2);
write(‘Enter the age, weight and height of student 3: ’);
readln(Age3, Weight3, Height3);
writeln(‘Age’:19, ‘Weight’:10, ‘Height’:10);
writeln(‘Student 1’, Age1:10, Weight1:10:1, Height1:10:1);
writeln(‘Student 2’, Age2:10, Weight2:10:1, Height2:10:1);
writeln(‘Student 3’, Age3:10, Weight3:10:1, Height3:10:1);
end.

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(U03C09L02Q003)
Declare the variables in the following statements and rewrite the statements as Pascal statements.
(a) Integer X is divided by integer Y and the remainder is stored in real variable R.
(b) Assign the result of integer M multiplying 5 to real variable P.
(c) String variable Tel is assigned to be ‘23423423’.
(d) Prompt a user to input a number and store the number in variable integer NUM. Then store the
square root of the input number in real variable SquareRoot.
(e) The variable C holds the value of the circumference. It is calculated by multiplying the real
constant Pi of value 3.14 to the diameter D, which is an integer variable.
(17 marks)

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

Answers
(a) var
R : real;
X, Y : integer;
begin
R := X mod Y;

(b) var
M : integer;
P : real;
begin
P := M * 5;

(c) var
Tel : string;
begin
Tel := ‘23423423’

(d) var
NUM : integer;
SquareRoot : real;
begin
write(‘Input a number: ’);
readln(NUM);
SquareRoot := sqrt(NUM);

(e) const
Pi = 3.14;
var
C : real;
D : integer;
begin
C := D * Pi;

Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(U03C09L02Q004)
In a series of numbers, the values of the first and the second terms are 0 and 1 respectively while the
value of any other term is calculated by adding the two previous terms. So, the first 10 terms are as
follows:

0,1,1,2,3,5,8,13,21,34,…

Write a program Series to find the value of any term for an input term number. (12 marks)

Sample output:
Enter a positive term number: -5
Non-positive number! Reenter: 0
Non-positive number! Reenter: 10
The value of term 10 is 34

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

Answer
program Series;
var
Tn, Tn1, Tn2 : real;
Term, n : integer;
begin
write(‘Enter a positive term number: ’);
readln(Term);
while Term < 1 do
begin
write(‘Non-positive number! Reenter: ’);
readln(Term)
end;
if Term = 1
then Tn := 0
else if Term = 2
                  then Tn := 1
                  else begin
                            Tn1 := 0;
                            Tn2 := 1;
                            for n := 3 to Term do
                                begin
                                    Tn := Tn1 + Tn2;
                                    Tn1 := Tn2;
                                    Tn2 := Tn
                                end
                         end;
    writeln(‘The value of term ’, Term, ‘ is ’, Tn:0:0)
end.

Computer & Information Technology for HKCEE 6 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(U03C09L02Q005)
Given that Stack is an array of string. Top is an integer variable. Item is a string variable. The
contents of Stack[1], Stack[2], Stack[3], Stack[4] and Stack[5] are
‘Peter’, ‘John’ ,‘Mary’ ,‘Joyce’ and ‘Leon’ respectively. The content of Top is
5.

(a) Refer to the procedure ListStack below. What is the output if the procedure ListStack
is executed?
procedure ListStack;
var
i : integer;
begin
for i := Top – 1 downto 1 do
writeln(‘ ’, Stack[i]:10, ‘ ’);
writeln(‘ ------------’)
end;

(b) Refer to the procedure Push below.


procedure Push;
begin
write(‘Enter data item: ’);
readln(Item);
Stack[Top] := Item;

Top := Top + 1;
end;

What is the output if the following statements are executed?



Push(‘Tim’);
ListStack;

(c) Refer to the procedure Pop below.
procedure Pop;
begin
Item := Stack[Top];
Top := Top – 1;
end;

Computer & Information Technology for HKCEE 7 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

What is the output if the following statements are executed?



Pop;
ListStack;

(d) What is the output if the following statements are executed?



Push(‘XXX’);
Pop;
Pop;
Push(‘YYY’);
Push(‘ZZZ’);
ListStack;

(9 marks)
Answers
(a)  Joyce 
 Mary 
 John 
 Peter 
------------

(b)  Tim 
 Joyce 
 Mary 
 John 
 Peter 
------------

(c)  Mary 
 John 
 Peter 
------------

Computer & Information Technology for HKCEE 8 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(d)  ZZZ 
 YYY 
 Mary 
 John 
 Peter 
------------
(U03C09L02Q006)
BranchName, Manager, NumOfStaff and Profit are 4 parallel arrays. They store the
information of 12 branches of a company.
(a) Write procedure ListBranch that lists out the details of all the branches.
(b) Write procedure FindManager that receives a branch name from users and then finds out the
manager of this branch. If the input branch is not present, the procedure will display a suitable
message.
(c) Write procedure MostStaff that finds out the branch with the greatest number of staff.
(d) Write procedure ProfitMoreThan that lists out the branch names and profit gained for
those branches with profit more than or equal to the input amount.
(24 marks)
Answers
(a) procedure ListBranch;
var
i : integer;
begin
writeln(‘Branch’:15, ‘Manager’:12, ‘Staff Number’:15,
‘Profit’:10)
for i := 1 to 12 do
writeln(BranchName[i]:15,Manger[i]:12,
NumOfStaff[i]:15,
Profit[i]:10:2);
end;

Computer & Information Technology for HKCEE 9 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(b) procedure FindManager;


var
i : integer;
Branch : string;
begin
write(‘Enter branch name: ’);
readln(Branch);
i := 1;
while (i < 12) and (BranchName[i] <> Branch) do
      i := i + 1;
   if BranchName[i] := Branch
      then writeln(‘The manager of’, BranchName[i], ‘ Branch 
is ’, 
                      Manager[i])
      else writeln(‘The branch is not present!’)
end;

(c) procedure MostStaff;
var   i, Greatest : integer;
begin
   Greatest := 1
   for i := 2 to 12 do
      if NumOfStaff[i] > NumOfStaff[Greatest]
         then Greatest := i;
   writeln(BranchName[Greatest], ‘ has the most staff.’)
end.

(d) procedure ProfitMoreThan;
var
   Amount : real;
   i : integer;
begin
   write(‘Enter an amount: ’);
   readln(Amount);
   writeln(‘Branch Name’:15, ‘Profit’:10);
   for i := 1 to 12 do
      if Profit[i] >= Amount
         then writeln(BranchName[i]:15, Profit[i]:10:2)

Computer & Information Technology for HKCEE 10 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 9 Formulation of Algorithms Question Bank

(U03C09L02Q007)
A and B are two arrays of type integer sorted in ascending order. Array A has m elements and array
B has n elements. Procedure InvertMerge merges the two arrays in descending order and stores
the result in array C. Write procedure InvertMerge. (15 marks)
Answer
procedure InvertMerge;
var
i, j, k : integer;
begin
i := m;
j := n;
while (i > 0) and (j > 0) do
      if A[i] > B[j]
         then begin
                  k := k +1;
                  C[k] := A[i];
                  i := i – 1
               end
         else begin
                   k := k + 1;
                   C[k] := B[j];
                   j := j – 1
               end;
   if i <> 0
      then while i <> 0 do
                begin
                    k := k + 1;
                    C[k] := A[i];
                    i := i – 1
                end
      else while j <> 0 do
                begin
                    k := k + 1;
                    C[k] := A[j];
                    j := j – 1
                end
end;

Computer & Information Technology for HKCEE 11 © Pearson Education Asia Limited 2004
(Module A2)

You might also like