Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q62 · 9618/23/M/J/25 Q3

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

Q629618/23/M/J/25original Q3Structured · 7 marks
Question 3 from 9618/23/M/J/25

Worked solution

Roll in a loop, add each roll to a sum, then divide by the number of rolls.

FUNCTION RollDice(NoOfTimes : INTEGER) RETURNS REAL
    DECLARE RollValue : INTEGER
    DECLARE Average : REAL
    DECLARE Count : INTEGER
    DECLARE Sum : INTEGER
    Sum ← 0
    FOR Count ← 1 TO NoOfTimes
        RollValue ← INT(RAND(6)) + 1
        OUTPUT RollValue
        Sum ← Sum + RollValue
    NEXT Count
    Average ← Sum / NoOfTimes
    RETURN Average
ENDFUNCTION

All variables declared, and Sum set to 0 [1]

Loop that runs NoOfTimes times [1]

RAND() used inside the loop [1]

INT() used inside the loop [1]

Random integer from 1 to 6: INT(RAND(6)) + 1 [1]

Each value output inside the loop [1]

Sum built up, average calculated, and the average (declared REAL, function RETURNS REAL) returned [1]

KEY IDEATotal up the rolls inside the loop, then divide by the number of rolls after it.
TOTAL 7 marks

Official mark scheme

Mark scheme for 9618/23/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.