June 2019
June 2019
Two Hours
Carry out ALL the tasks given. For your guidance, the approximate mark for each part of a task is indicated
in brackets.
Great importance is attached to the accuracy, layout and labelling of drawings and computer generated
outputs.
You are reminded of the necessity for good English and orderly presentation of your answers.
Write algorithms in the answer booklet provided. Also record in your answer booklet any information
requested or that you believe would make it easier to understand how you carried out tasks or answered
questions.
You are expected to print out a single copy of relevant fragments of your program at different times.
Please notify the instructor of any required printout that was not done!
When an imperative programming language is required to write program code, either Standard [ISO]
Pascal or the [ANSI] C or C11 programming languages may be used.
If need be supervisors will assist you in recording details of intermediate work carried out on the
computer.
Do not write on the first page of your answer booklet. It is reserved for administrative purposes.
Where information is provided as soft copy, notify the instructors if it is not found in your machine or has
not been made available to you.
Turn Over
Task 1
A two-dimensional array, A, has N rows and N columns, where N is a positive integer. The following
algorithm is written to fill array A with the numbers 1,2,3,.., N².
N=input(Enter an integer greater than zero)
K=1
loop for ROW-0 to N-1
loop for COLUMNS to N-1
A[ROW][COLUMN]=K
K=K+1
end loop
end loop
Figure 1.
1. In your answer booklet, give the values laid out in a 2 - D array, for N=3, as obtained from the
algorithm. In other words, use the algorithm to insert values in the array, and give a trace of the
order in which the values are inserted into the array. (5 marks)
Task 2
2. There are many different ways of placing the numbers 1 to N² into an N x N two-dimensional
array. The following two-dimensional array, with dimensions 5X5, has been filled in a circular (spiral)
pattern with numbers 1 to 52.
An algorithm to fill an NXN two-dimensional array, in a circular (spiral) pattern,
with numbers from 1 to N is given as follows:
initialize Z= 1,
initialize TOP, BOTTOM, LEFT and RIGHT.
iterate until the whole array is filled.
each time Z is placed correctly increase the value of Z by 1.
fill the elements of the TOP row starting from LEFT to RIGHT.
increase TOP by 1 before filling the elements of the RIGHT column.
fill the elements of the RIGHT column starting from TOP to BOTTOM.
decrease RIGHT by l before filling the elements of the BOTTOM row.
and continue filling the BOTTOM row and LEFT column in a similar way. adjusting TOP,
RIGHT, BOTTOM and LEFT accordingly.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Figure 2
(i) your answer booklet, State the initial values for TOP, BOTTOM, LEFT and RIGHT. (1 mark)
(ii) State the consequence of not increasing TOP by l before starting to fill the elements of the RIGHT
column. (1 mark)
(ii) In the algorithm described above, state the indices (subscripts) of the first and the last element to
be filled in the BOTTOM row. (1 mark)
SpiralOrder
Begin
k=l; initialise top, bottom, left and right;
while k<N*N do
call FillRowForward(A, top, left, right, k);
increment Top by 1;
call FillColumnDownward(A, top, bottom, right, k); decrement right by 1;
call FillRowBacwad(A, bottom, left, right, k); decrement Bottom by 1;
call FillColumnUpward (A, top, bottom, left, k); increment Left by 1;
Endwhile
End
Figure 3.
Task 4
To test your program;
5. Modify and implement the algorithm in Figure 1, such that instead of inserting the numbers into
the matrix, it should print the numbers already inserted in the matrix, line after line, with equal
spaces between the numbers. After each line is printed the cursor should go to the next line.
(4 marks)
6. Save and print your code, run the program and print the output. (2 marks)
7. Adapt the PL procedures in 3i, i, ii and iv such that instead of inserting the number z into the array
cells, they should rather print the values found in the cells. That is:
Adapt FillRowBacward(A, bottom, left, right, z) to PrintRowBacward(A, bottom, left, right) so that it
prints the bottom row. (2 marks)
Task 5
10. In your answer booklet, explain briefly how you would adapt each of the printprocedures:
PrintRowForward, PrintRowBacward, PrintColumnDownward, PrintColumnUpward such that
printing is done in the reverse order of insertion. (4 marks)
11. Put the print procedures mentioned in(10) in the order in which they should be called to the
procedure PrintSpiralOrder, such that the printing effectively is in the reverse order when the
program runs. (1 mark)
END