Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q61 · 9618/22/M/J/25 Q3

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

Q619618/22/M/J/25original Q3Structured · 8 marks
Question 3 from 9618/22/M/J/25

Worked solution

Make the first number, then keep making the second one until it is different. Check both are negative before making the third.

DECLARE FirstNumber : INTEGER
DECLARE SecondNumber : INTEGER
DECLARE ThirdNumber : INTEGER
FirstNumber ← INT(RAND(21)) - 10
OUTPUT FirstNumber
REPEAT
    SecondNumber ← INT(RAND(21)) - 10
UNTIL FirstNumber <> SecondNumber
OUTPUT SecondNumber
IF FirstNumber < 0 AND SecondNumber < 0 THEN
    ThirdNumber ← INT(RAND(6)) + 30
    OUTPUT ThirdNumber
ENDIF

All variables declared [1]

RAND() used [1]

INT() used [1]

Conditional loop until two different numbers are generated [1]

INT(RAND(21)) - 10 gives a number from -10 to 10 inclusive [1]

Both different random integers output [1]

Test that both numbers are negative [1]

If so, a third random integer from 30 to 35 inclusive is generated and output [1]

KEY IDEARAND(n) gives 0 up to n, so scale with INT and add or subtract to reach the range.
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/22/M/J/25 question 3
VIDEO
▶ Whole paper

Video solution of the whole paper · by Awab Aqib. 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.