Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q37 · 9618/22/M/J/25 Q7

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

Q379618/22/M/J/25original Q7Structured · 18 marks
Question 7 from 9618/22/M/J/25

Worked solution

(a)

Check the ID is in range. Then loop through the rows. Return the points if the ID matches. Stop with -1 if a row holds 99999, because the array is sorted and unused rows are at the end, so the ID cannot be further on.

FUNCTION FindCustomer(CustomerID : INTEGER) RETURNS INTEGER
    DECLARE Index : INTEGER
    IF CustomerID < 10001 OR CustomerID > 11000 THEN
        RETURN -1
    ENDIF
    FOR Index ← 1 TO 1000
        IF Loyalty[Index, 1] = CustomerID THEN
            RETURN Loyalty[Index, 2]
        ENDIF
        IF Loyalty[Index, 1] = 99999 THEN
            RETURN -1
        ENDIF
    NEXT Index
    RETURN -1
ENDFUNCTION

Function header and ENDFUNCTION with the correct parameter and return type [1]

Check the ID is in range 10001 to 11000 and return -1 if not [1]

A FOR loop (or a conditional loop) [1]

Loop covers rows 1 to 1000 [1]

Check whether the current row holds the customer ID, inside the loop [1]

If found, return the loyalty points [1]

Stop the loop when a row holds 99999 (and return -1) [1]

Return -1 if the ID is not found [1]

Other correct ways also score: a WHILE loop with a Found flag, a direct index of CustomerID - 10000, or a binary search.

(b)

Loop through the used rows. Output the ID when the points are 11 or more. Add up the points and count the customers, then work out the average after the loop.

PROCEDURE PointsReport()
    DECLARE Count, Index, Sum : INTEGER
    DECLARE Average : REAL
    Index ← 1
    Count ← 0
    Sum ← 0
    WHILE Index <= 1000 AND Loyalty[Index, 1] <> 99999
        IF Loyalty[Index, 2] >= 11 THEN
            OUTPUT Loyalty[Index, 1]
        ENDIF
        Count ← Count + 1
        Sum ← Sum + Loyalty[Index, 2]
        Index ← Index + 1
    ENDWHILE
    Average ← Sum / Count
    OUTPUT "The average points of all the customers is ", Average
ENDPROCEDURE

Procedure header and ending [1]

Index, Sum and Count declared and set to start values [1]

Loop through the Loyalty rows, ending at 99999 or row 1000 [1]

Check whether the loyalty points are 11 or more, inside the loop [1]

Output the customer ID when true [1]

Add the points to Sum and add 1 to Count inside the loop [1]

Work out and output the average with a suitable message [1]

(c)(i)

An array can only store data of one data type, so it cannot hold a STRING ID and INTEGER points together. [1]

(c)(ii)

Either of these ways scores:

Define a new record type (composite data type) with a STRING field and an INTEGER field [1]

Declare a 1D array whose elements are of this record type [1]

Or: turn the loyalty points into a string and join it to the customer ID

and store each joined string in a 1D array of STRING (this alternative scores the same two marks)

KEY IDEAA 2D array holds one data type only; use a record type or join values into a string. Sorted data with a marker lets a search stop early.
TOTAL 18 marks

Official mark scheme

Mark scheme for 9618/22/M/J/25 question 7
VIDEO
▶ Whole paper

Video solution of the whole paper · by Awab Aqib. 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.