Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q42 · 9618/23/O/N/25 Q8

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

Q429618/23/O/N/25original Q8Structured · 20 marks
Question 8 from 9618/23/O/N/25

Worked solution

(a)

Loop through the Loan array and stop as soon as the matching loan is found (this is what makes it efficient, because each combination occurs only once). Check both StudentID and BookID, then check OnLoan. After the loop, give the warning if nothing was found.

PROCEDURE LoanStatus(ThisStudentID : STRING, ThisBookID : STRING)
    DECLARE Index : INTEGER
    DECLARE Found : BOOLEAN
    Index ← 1
    Found ← FALSE
    WHILE Index <= 8000 AND NOT Found
        IF Loan[Index].StudentID = ThisStudentID AND Loan[Index].BookID = ThisBookID THEN
            IF Loan[Index].OnLoan = FALSE THEN
                OUTPUT "Loan has been returned."
            ELSE
                OUTPUT "Loan has not been returned."
            ENDIF
            Found ← TRUE
        ENDIF
        Index ← Index + 1
    ENDWHILE
    IF NOT Found THEN
        OUTPUT "Warning - Loan not found."
    ENDIF
ENDPROCEDURE

Procedure heading with the two STRING parameters, and ENDPROCEDURE [1]

Loop through the elements of the Loan array (index 1 to 8000) [1]

The loop also stops once the loan is found (a Found flag in the condition) [1]

Inside the loop, test for the correct StudentID [1]

... and the correct BookID [1]

Check the OnLoan data item of that record [1]

Output the right message (returned or not returned) once, for the loan found [1]

If no matching loan is found, output a warning after the loop [1]

A loop that starts at 0 and ends at 7999 is also accepted.

(b)

Count the records whose tutor ID matches and whose OnLoan is TRUE. The tutor ID is the first three characters of StudentID, so use LEFT. Unused records have an empty StudentID, so they never match and are skipped.

FUNCTION LoansPerTutor(TutorID : STRING) RETURNS INTEGER
    DECLARE Index, Count : INTEGER
    Count ← 0
    FOR Index ← 1 TO 8000
        IF LEFT(Loan[Index].StudentID, 3) = TutorID AND Loan[Index].OnLoan = TRUE THEN
            Count ← Count + 1
        ENDIF
    NEXT Index
    RETURN Count
ENDFUNCTION

Local integer variable Count set to 0 [1]

Loop through all the elements of the Loan array [1]

Refer to a data item of Loan[Index] inside the loop, using dot notation [1]

Extract the tutor ID from Loan[Index].StudentID inside the loop [1]

Test for a matching tutor ID AND OnLoan = TRUE, using correct dot notation, inside the loop [1]

If both are true, add 1 to Count inside the loop [1]

RETURN Count after the loop [1]

(c)(i)

Make the name change every time the program runs, and make it say what the file is.

Include the date of the archive (or the next number in a sequence) in the name, for example 20251104 [1]

Join this to a fixed root name such as Archive, for example Archive20251104 [1]

(c)(ii)

Write: the OnLoan item is a BOOLEAN, and a text file can only hold text (strings), so it must be converted to a string before it is written [1]

(c)(iii)

Benefit: the algorithm to write and read a record is simpler, with no need to join data items into one string or split (unpack) them afterwards [1]

Drawback: more file accesses (one for each data item) are needed to read or write a whole record [1]

KEY IDEASearch the array with a loop and a Found flag, use dot notation for record fields, and remember that text files only hold strings.
TOTAL 20 marks

Official mark scheme

Mark scheme for 9618/23/O/N/25 question 8
VIDEO
▶ Topic lesson

A lesson on this topic · by ZAINEMATICS. 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.