Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
Make the random number first. Then repeat: ask, check high, check low. Stop when the guess matches.
DECLARE GeneratedValue : INTEGER
DECLARE Guess : INTEGER
GeneratedValue ← INT(RAND(100)) + 1
REPEAT
OUTPUT "Enter a guess between 1 and 100: "
INPUT Guess
IF Guess > GeneratedValue THEN
OUTPUT "Guess was too high"
ENDIF
IF Guess < GeneratedValue THEN
OUTPUT "Guess was too low"
ENDIF
UNTIL Guess = GeneratedValue
OUTPUT "Number guessed correctly"All variables declared [1]
RAND() function used [1]
INT() function used [1]
Correct random number from 1 to 100: INT(RAND(100)) + 1 [1]
Conditional loop that runs until the number is guessed [1]
Prompt and input a guess inside the loop [1]
Check if the guess is too high, with a message, inside the loop [1]
Check if the guess is too low, with a message, inside the loop [1]
The correct-guess message after the loop also scores. Any eight of the nine points get the 8 marks.
▶ Whole paper
Video solution of the whole paper · by Sir Sikandar Raza. 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.