9618/21/M/J/24 question 4: worked solution (May/June 2024, paper 21)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q27 9618/21/M/J/24 original Q4 Structured · 6 marks Worked solution (a) Use one loop through all 100 elements. Add each element to the odd total or the even total, using MOD 2 to decide. Then compare the totals after the loop. FUNCTION Check() RETURNS STRING DECLARE Odd, Even, Index : INTEGER Odd ← 0 Even ← 0 FOR Index ← 1 TO 100 IF Index MOD 2 = 0 THEN Even ← Even + Data[Index] ELSE Odd ← Odd + Data[Index] ENDIF NEXT Index IF Odd > Even THEN RETURN "Odd" ELSE IF Even > Odd THEN RETURN "Even" ELSE RETURN "Same" ENDIF ENDIF ENDFUNCTION Function heading, RETURNS STRING and ENDFUNCTION [1] Local variables Odd, Even and Index declared as INTEGER [1] Odd and Even initialised to 0 [1] Loop that runs 100 times (through the array) [1] Odd and Even totals added up inside the loop [1] Totals compared after the loop and the correct string returned [1] KEY IDEA Add up two totals in a single loop, then compare them after the loop to choose what to return. TOTAL 6 marks Official mark scheme VIDEO ▶ Starts at Q4 (32:47) Starts at Q4 (32:47) · by Pseudocode Pro. Tap the picture to open it on YouTube.