Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Pick a random index and repeat until the element is not an empty string. Do this for the first filling, then for the second filling (which must also differ from the first), then for the bread. Use INT(RAND(n)) + 1 to get an index from 1 to n.
PROCEDURE Special()
DECLARE Index : INTEGER
DECLARE Filling1, Filling2 : STRING
REPEAT
Index ← INT(RAND(35)) + 1
UNTIL Filling[Index] <> ""
Filling1 ← Filling[Index]
REPEAT
Index ← INT(RAND(35)) + 1
UNTIL Filling[Index] <> "" AND Filling[Index] <> Filling1
Filling2 ← Filling[Index]
REPEAT
Index ← INT(RAND(10)) + 1
UNTIL Bread[Index] <> ""
OUTPUT "The daily special is ", Filling1, " and ", Filling2, " on ", Bread[Index], " bread."
ENDPROCEDURELoop for the first filling that avoids unused elements [1]
Loop for the second filling that avoids unused elements [1]
Check that the second filling is different from the first (compare contents or indices) [1]
Loop for the bread that avoids unused elements [1]
Uses RAND(35) for fillings and RAND(10) for bread [1]
RAND used completely correctly everywhere, with INT() and + 1 [1]
Correct output, once only, after the fillings and bread have been chosen [1]
Store the rules that stop two fillings going together.
For each filling, keep a list of incompatible fillings (or indices) [1]
When choosing the second filling, also check that it is not on the first filling's list [1]
Another way that scores: keep a list of good combinations and pick one at random (this gets both marks).
▶ Whole paper
Video solution of the whole paper · by Papersdock. 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.