Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q41 · 9618/22/O/N/25 Q8

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

Q419618/22/O/N/25original Q8Structured · 18 marks
Question 8 from 9618/22/O/N/25

Worked solution

(a)

Loop through all 7000 records. For this student, add to the on-loan count or the returned count.

PROCEDURE CountLoans(ThisStudentID : STRING)
    DECLARE Index, LCount, RCount : INTEGER
    LCount ← 0
    RCount ← 0
    FOR Index ← 1 TO 7000
        IF Loan[Index].StudentID = ThisStudentID THEN
            IF Loan[Index].OnLoan = FALSE THEN
                RCount ← RCount + 1
            ELSE
                LCount ← LCount + 1
            ENDIF
        ENDIF
    NEXT Index
    OUTPUT "Student has ", LCount, " books on loan"
    OUTPUT "and has returned ", RCount, " books"
ENDPROCEDURE

Index and two count variables declared [1]

Loop for 7000 passes [1]

Indexed record item with correct dot notation, such as Loan[Index].StudentID [1]

StudentID field compared with the parameter, inside the loop [1]

OnLoan tested, with correct logic for both outcomes [1]

Both counts set to 0 before the loop, and the correct one increased inside the loop [1]

Both counts output with a suitable message [1]

(b)

Look for the first unused record (StudentID is empty). Fill it in and stop at once.

FUNCTION NewLoan(ThisStudentID, ThisBookID : STRING) RETURNS BOOLEAN
    DECLARE Index : INTEGER
    DECLARE IsStored : BOOLEAN
    CONSTANT Blank = ""
    Index ← 1
    IsStored ← FALSE
    WHILE Index <= 7000 AND NOT IsStored
        IF Loan[Index].StudentID = Blank THEN
            Loan[Index].StudentID ← ThisStudentID
            Loan[Index].BookID ← ThisBookID
            Loan[Index].OnLoan ← TRUE
            IsStored ← TRUE
        ENDIF
        Index ← Index + 1
    ENDWHILE
    RETURN IsStored
ENDFUNCTION

Loop through the 7000 elements with a correct loop structure [1]

Loop stops at the first unused element (or a RETURN inside a FOR loop) [1]

Correct use of indexed dot notation [1]

Test whether the record is unused: StudentID = "" [1]

StudentID and BookID assigned from the parameters [1]

OnLoan assigned TRUE [1]

Correct BOOLEAN returned in both cases (space found, no space) [1]

A FOR loop with RETURN TRUE inside and RETURN FALSE after the loop also scores.

(c)

Save the records to a text file (open it, write, close it) [1]

Either write each record as one line, joining the items with a separator character, or write each item on its own line [1]

(d)

Data: store the date the book is due back (or the start date and loan period), or store the student's email address. [1]

Use: calculate three days before the due date and compare it with today's date, then send the email to the student's stored address. [1]

KEY IDEASearch an array of records with dot notation; to save data for later, write it to a file.
TOTAL 18 marks

Official mark scheme

Mark scheme for 9618/22/O/N/25 question 8
VIDEO
▶ Whole paper

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