9618/23/O/N/23 question 4: worked solution (October/November 2023, 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. Q49 9618/23/O/N/23 original Q4 Structured · 7 marks Worked solution (a) Keep a base value. Each time, add a random amount from 0 up to just below the step size, so the new number is always bigger than the old one. Then raise the base by the step: PROCEDURE RandList() DECLARE Count, BaseNum, ThisNum : INTEGER CONSTANT StepVal = 10 BaseNum ← 0 FOR Count ← 1 TO 25 ThisNum ← BaseNum + INT(RAND(StepVal)) OUTPUT ThisNum BaseNum ← BaseNum + StepVal NEXT Count ENDPROCEDURE Procedure heading and ending [1] Loop counter Count declared as a local INTEGER [1] Loop that runs 25 times (one number each time) [1] Random number made with INT and RAND inside the loop [1] Number made greater than the previous one, and the 'previous' value updated (here BaseNum increases by StepVal) [1] Random number output inside the loop [1] (b) The condition says: the next value is equal to or smaller than the current value. Write the same test as one comparison: IF Result[x + 1] <= Result[x] THEN [1] Result[x] >= Result[x + 1] also scores. KEY IDEA Make each number bigger than the last by adding a random part to a base that grows every time. TOTAL 7 marks Official mark scheme VIDEO ▶ Starts at Q4 (55:26) Starts at Q4 (55:26) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.