User interface language: English | Español

Date November 2019 Marks available 2 Reference code 19N.1.HL.TZ0.13
Level HL Paper 1 Time zone no time zone
Command term Deduce Question number 13 Adapted from N/A

Question

The following matrix has non-zero elements on the diagonal, on the super-diagonal (the first diagonal above the main diagonal) and on the sub-diagonal (the first diagonal below the main diagonal). All the rest of the elements are zeros.

The following two-dimensional array named MAT of dimensions 6 × 6 is an example of such a matrix.

MAT

Method isValidMatrix(N,A) accepts an integer N and a two-dimensional array A of dimensions NxN. It returns True if all elements below the subdiagonal and all elements above the superdiagonal are zeros and all elements on three diagonals are non-zeroes; otherwise it returns False.

For example, isValidMatrix(6,MAT) returns True for the matrix MAT given above.

Given the following recursive method mystery() with two formal parameters:
A (a two-dimensional array) and R (an integer).

mystery(A,R)
if R > 0 then
return A[R][R-1] + mystery(A,R-1)
else
return 0
end if
end mystery

State the value of MAT[3][4].

[1]
a.

Construct an efficient algorithm for the method isValidMatrix().

[8]
b.

Determine the value of variable X after execution of the following method call:

X = mystery(MAT,5)

where MAT is the two-dimensional array given. You must show your working.

[4]
c.

Deduce the purpose of the method mystery(A,R).

[2]
d.

Markscheme

Award [1 max]
4;

a.

Award [8 max]
Award [1] for initialization, correct changing and returning of a flag.
Award [1] for the nested loops.
Award [1] for correct initial and for correct terminal value and changing the value of the control variable in outer loop.
Award [1] for correct initial and for correct terminal value and changing the value of the control variable in inner loop.
Award [1] for efficiency (terminating execution when a zero or non-zero element is not at the correct position).
Award [1] for correct condition in if statement.
Award [1] for checking elements in the lower/upper triangle.
Award [1] for checking elements on the three diagonals.
Award [1] for the correct logical expression.

Example 1:

INVALID=False
R=0
loop while R<N and not INVALID
C=0
loop while C<N and not INVALID
if abs(R-C)>=2 and A[R][C]!=0 or abs(R-C)<2 and
A[R][C]==0 then
INVALID=True
endif
C=C+1
endwhile
R=R+1
endwhile
return not INVALID

Please note that instead the logical expression given in the Example answer 1 several if statements could be used and award 1 mark for each correct if statement (1 mark for checking elements in the lower triangle, 1 mark for checking upper triangle, 1 mark for checking three diagonals).

if R>C and R-C>1 then //lower triangle
if A[R][C]!=0 then
INVALID=True
endif
endif
if R<C and C-R>1 then //upper triangle
if A[R][C]!=0 then
INVALID=True
endif
endif
if R<C and C-R=1 or R>C and R-C==1 or R==C then //three
diagonals
if A[R][C]==0 then
INVALID=True
endif
endif

Example 2:
Award [7 max] (no ‘efficiency’ mark).
Award [1] for initialization, correct changing and returning of a flag
Award [1] for the nested loops.
Award [1] for correct initial and for correct terminal value of the control variable in outer loop.
Award [1] for correct initial and for correct terminal value of the control variable in inner loop.
Award [1] for correct condition in if statement.
Award [1] for checking elements in lower/upper triangle.
Award [1] for checking elements on the three diagonals.
Award [1] for the use of correct logical expressions.

INVALID=False
loop R=0 to N-1 do
loop C=0 to N-1 do
if abs(R-C)>=2 and A[R][C]!=0 or abs(R-C)<2 and
A[R][C]==0 then
INVALID=True
endif
endloop
endloop
return not INVALID
b.
Award [4 max]
X = mystery( MAT, 5)= 5+ mystery( MAT, 4);
5 + 7 + mystery( MAT, 3);
5 + 7 −5 + mystery( MAT, 2);
5 + 7 −5 + 9 + mystery( MAT, 1);
5+7-5+9+1+mystery(MAT, 0)= 5+7-5+9+1+0 = 17;
c.

Award [2 max]
Calculates the sum of R elements;
Starting from A[R][R-1] to A[1][0];
on the sub-diagonal (of the two-dimensional array A);

d.

Examiners report

Parts (a) and (b) have been attempted with some repetition. The reasonable general knowledge of when and how data can be lost and backup strategies. However, many responses were vague enough not to describe the situation with reference to the basic technical terminology of the devices/systems. 

a.

The question was reasonably well answered with many gaining at least 2 or 3 of the marking points.

b.

Quite a few candidates did not attempt the recursion question. However, those who did generally answered well.

c.

Quite a few candidates did not attempt the recursion question. However, those who did generally answered well.

d.

Syllabus sections

Topic 5: Abstract data structures » 5.1 Abstract data structures
Show 80 related questions
Topic 5: Abstract data structures

View options