Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q27 · 9618/21/M/J/24 Q4

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.

Q279618/21/M/J/24original Q4Structured · 6 marks
Question 4 from 9618/21/M/J/24

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 IDEAAdd up two totals in a single loop, then compare them after the loop to choose what to return.
TOTAL 6 marks

Official mark scheme

Mark scheme for 9618/21/M/J/24 question 4
VIDEO
▶ Starts at Q4 (32:47)

Starts at Q4 (32:47) · by Pseudocode Pro. 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.