9618/23/M/J/25 question 3: worked solution (May/June 2025, paper 23)

Show as plain text

Cambridge AS Level Computer Science 9618 · 11.2 Constructs Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3. Q62 9618/23/M/J/25 original Q3 Structured · 7 marks 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 IDEA Total up the rolls inside the loop, then divide by the number of rolls after it. TOTAL 7 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.