9618/22/M/J/25 question 3: worked solution (May/June 2025, paper 22)
Show as plain textHide 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. Q61 9618/22/M/J/25 original Q3 Structured · 8 marks 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 IDEA RAND(n) gives 0 up to n, so scale with INT and add or subtract to reach the range. TOTAL 8 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.