9618/21/M/J/25 question 7: worked solution (May/June 2025, paper 21)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.3 Files Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4. Q64 9618/21/M/J/25 original Q7 Structured · 18 marks Worked solution (a) Add the coffees ordered to the points. While there are at least 11 points and coffees left, give one free coffee and take off 11 points. FUNCTION CustomerOrder(Number : INTEGER, Points : INTEGER) RETURNS INTEGER DECLARE NewPoints, NumberFree : INTEGER NewPoints ← Points + Number NumberFree ← 0 WHILE NewPoints >= 11 AND Number > 0 NumberFree ← NumberFree + 1 NewPoints ← NewPoints - 11 Number ← Number - 1 ENDWHILE OUTPUT "Number of free coffees is: ", NumberFree RETURN NewPoints ENDFUNCTION Function heading, parameters, return type and ENDFUNCTION [1] Number of coffees ordered added to the points [1] Correct test for a free coffee (points above 10, that is 11 or more) and 11 taken off [1] Repeated for multiple free coffees, with the free coffees counted and NewPoints reduced each time [1] Number of free coffees output with a suitable message [1] New points total returned [1] (b)(i) Read to the last line to find the highest ID, then append new lines. PROCEDURE AddNewCustomers(NumToAdd : INTEGER) DECLARE CustomerID, Count : INTEGER DECLARE Line, NewLine : STRING OPENFILE "Loyalty.txt" FOR READ WHILE NOT EOF("Loyalty.txt") READFILE "Loyalty.txt", Line ENDWHILE CLOSEFILE "Loyalty.txt" CustomerID ← STR_TO_NUM(LEFT(Line, 6)) OPENFILE "Loyalty.txt" FOR APPEND FOR Count ← 1 TO NumToAdd CustomerID ← CustomerID + 1 OUTPUT CustomerID NewLine ← NUM_TO_STR(CustomerID) & ",0" WRITEFILE "Loyalty.txt", NewLine NEXT Count CLOSEFILE "Loyalty.txt" ENDPROCEDURE All variables declared with correct types, including a string and an integer [1] File opened in read mode and closed before it is opened in append mode [1] Conditional loop using EOF that reads each line, so the last line is kept [1] File opened in append mode and closed afterwards [1] Customer ID taken from the last line and converted to an integer (or worked out from a count of records) [1] A count-controlled loop for the number of customers to add [1] In the loop, the ID is increased by 1 and output [1] In the loop, a string for the new customer is made and written to the file [1] (b)(ii) The module must cope with an empty or missing file. Check whether Loyalty.txt exists or is empty [1] If it does not exist, create it using write mode [1] If the file is new or empty, the first customer ID is 100001 , and "100001,0" is written to the file [1] For every other customer, use the existing code as before [1] KEY IDEA Find the last ID in the file, then add one per new customer; an empty file needs its first ID of 100001 set separately. TOTAL 18 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Sir Sikandar Raza. Tap the picture to open it on YouTube.