Date | November 2017 | Marks available | 4 | Reference code | 17N.1.SL.TZ0.7 |
Level | SL | Paper | 1 | Time zone | no time zone |
Command term | Construct | Question number | 7 | Adapted from | N/A |
Question
Consider the following algorithm, where N
is a positive integer.
loop for K from 1 to N
loop for J from 1 to N
if K = J then
output K
end if
end loop
end loop
Construct the algorithm which performs the same task using a single while
loop, instead of nested for
loops.
Markscheme
Award marks as follows up to [4 max].
Award [1] for correct initialization.
Award [1] for correct condition.
Award [1] for correct output.
Award [1] for changing the value of controlling variable.
Note: The question explicitly requires one while
loop.
Do no not accept a for
loop. Do not accept two while
loops.
Example answer
K = 1
loop while K <= N
output K
K = K + 1
end loop
Example answer (including J variable)
K = 1
J = 1
loop while K = J AND K <= N
output K
K = K + 1
J = J + 1
end loop
Examiners report
[N/A]