Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
Run the procedure one line at a time. Index goes 1, 2, 3, 4, 7, 4, 1, 3, 4. At CaseVar 5 the OTHERWISE sets Count to 20. The loop ends when Count reaches 22.
| Index | CaseVar | Count | Num[1] | Num[2] | Num[3] | Num[4] |
|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 2 | 5 | 3 | |
| 1 | ||||||
| 2 | 1 | 2 | ||||
| 3 | ||||||
| 5 | ||||||
| 20 | ||||||
| 4 | ||||||
| 3 | ||||||
| 7 | 19 | 6 | ||||
| 4 | ||||||
| 6 | ||||||
| 20 | ||||||
| 1 | ||||||
| 2 | ||||||
| 3 | 22 | 3 | ||||
| 4 |
First three rows correct: starting values, CaseVar 1, then Index 2, Count 1 and Num[1] = 2 [1]
Index column correct from Index 3 downwards [1]
CaseVar column correct from 5 downwards [1]
Count column correct from 20 downwards [1]
Num[4] = 6 when Index is 7, and Num[1] = 3 when Index is 3 the second time [1]
Clauses for CaseVar 1 and 2. [1]
Both clauses add Index to the element. The only difference is the step, which equals CaseVar, so use CaseVar instead of the numbers:
1 TO 2 : Num[Index] ← Num[Index] + Index
Index ← Index + CaseVar
Count ← Count + CaseVarCaseVar used instead of the fixed numbers 1 and 2 [1]
Whole clause correct [1]
▶ Topic lesson
A lesson on this topic · by ZAINEMATICS. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.