Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Loop through all the loans and count those for this student that are still on loan. Stop as soon as the count reaches 5.
PROCEDURE OKToBorrow(ThisStudentID : STRING)
DECLARE Index, Count : INTEGER
CONSTANT Max = 5
Count ← 0
Index ← 1
WHILE Index <= 5000 AND Count < Max
IF Loan[Index].StudentID = ThisStudentID AND Loan[Index].OnLoan = TRUE THEN
Count ← Count + 1
ENDIF
Index ← Index + 1
ENDWHILE
IF Count < Max THEN
OUTPUT "Student may borrow another book"
ELSE
OUTPUT "Student may not borrow another book"
ENDIF
ENDPROCEDUREProcedure heading, parameter and ending [1]
Loop through the elements of the Loan array [1]
Loop ends early when the maximum of 5 is reached [1]
Test that StudentID matches the parameter, using dot notation, inside the loop [1]
... and that OnLoan = TRUE, inside the loop [1]
If both are true, Count is increased by 1 [1]
After the loop, a suitable message is output in both cases [1]
Loop through the loans looking for the right student and book. When found, return at once: TRUE if it was still on loan (after setting OnLoan to FALSE), FALSE if it was already returned. If the loop ends, the loan was not found.
FUNCTION ReturnBook(ThisStudentID, ThisBookID : STRING) RETURNS BOOLEAN
DECLARE Index : INTEGER
Index ← 1
WHILE Index < 5001
IF Loan[Index].StudentID = ThisStudentID AND Loan[Index].BookID = ThisBookID THEN
IF Loan[Index].OnLoan = TRUE THEN
Loan[Index].OnLoan ← FALSE
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDIF
Index ← Index + 1
ENDWHILE
RETURN FALSE
ENDFUNCTIONLoop through the elements of the Loan array [1]
Loop that ends when the loan is not found (all elements checked) [1]
A single data item of a record referenced, such as Loan[Index].StudentID, inside the loop [1]
Correct test of both StudentID and BookID inside the loop [1]
If the book is not yet returned, OnLoan is set to FALSE [1]
RETURN TRUE when the loan is found and not already returned [1]
RETURN FALSE when the loan is found but already returned [1]
RETURN FALSE when the loan is not found [1]
One mark from each group:
Record change: add new data items to the loan record, for example the date due back, or the loan date and the loan length category [1]
Program change: use the book's category to work out the due date, and compare the due date with today's date to see if it is overdue or to work out the fine [1]
Another way: a new record for the categories with a loan length field, and mapping each category to its loan length.
▶ Whole paper
Video solution of the whole paper · by CSFeedbySirNisarAli. 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.