Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
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
ENDFUNCTIONAll 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]
▶ 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.