Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 8 of 8.
Follow the flowchart. It searches ClassList and gives the position, or -1 if the name is not found.
| Name | OUTPUT |
|---|---|
| Ridwan | 5 |
| Yan | -1 |
| Ella | 3 |
Any two rows correct [1]
All three rows correct [1]
Write: linear search [1]
Set Position and Count, input the name once, then loop through the array until the end or until the name is found.
Position ← -1
Count ← 1
OUTPUT "Please enter a name "
INPUT Name
REPEAT
IF Name = ClassList[Count]
THEN
Position ← Count
Count ← 6
ELSE
Count ← Count + 1
ENDIF
UNTIL Count > 5
OUTPUT PositionPosition and Count set before the loop [1]
Name input before (outside) the loop [1]
A suitable loop with a correct condition [1]
Name compared with ClassList[Count] [1]
Count increased by 1 each time [1]
Position output (the position found, or -1) [1]
A WHILE loop, or a FOR loop from 1 to 5, is also accepted.
▶ Topic lesson
A lesson on this topic · by Mr Bulmer's Learning Zone. 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.