Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q59 · 9618/21/M/J/25 Q3

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.

Q599618/21/M/J/25original Q3Structured · 8 marks
Question 3 from 9618/21/M/J/25

Worked solution

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.

KEY IDEAGenerate the random number once, then loop with input and comparison until the guess equals it.
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/21/M/J/25 question 3
VIDEO
▶ 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.