Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Read each line of Stock.txt. Take characters 5 to 7 as the code. If it equals Code1, rebuild the line with Code2 and add 1 to Count. Write every line to NewStock.txt.
FUNCTION ChangeSupp(Code1, Code2 : STRING) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE ThisLine, ThisCode : STRING
OPENFILE "Stock.txt" FOR READ
OPENFILE "NewStock.txt" FOR WRITE
Count ← 0
WHILE NOT EOF("Stock.txt")
READFILE "Stock.txt", ThisLine
ThisCode ← MID(ThisLine, 5, 3)
IF ThisCode = Code1 THEN
ThisLine ← LEFT(ThisLine, 4) & Code2 & RIGHT(ThisLine, LENGTH(ThisLine) - 7)
Count ← Count + 1
ENDIF
WRITEFILE "NewStock.txt", ThisLine
ENDWHILE
CLOSEFILE "NewStock.txt"
CLOSEFILE "Stock.txt"
RETURN Count
ENDFUNCTIONOpen both files in the correct modes and close them afterwards [1]
Conditional loop until EOF("Stock.txt") [1]
Read a line and extract ThisCode inside the loop [1]
Test ThisCode = Code1 and, if true, increase Count (set to 0 first) inside the loop [1]
Update ThisLine using substring functions and & inside the loop [1]
The update of ThisLine is completely correct [1]
Write ThisLine to NewStock.txt inside the loop [1]
Return Count after the loop [1]
Output the header, then read every line. When the code matches, output the item number and description and add 1 to Count. Output the count at the end.
PROCEDURE Report_1(Supp : STRING)
DECLARE Count : INTEGER
DECLARE ThisItemNum, ThisDesc, ThisLine, ThisCode : STRING
Count ← 0
OPENFILE "Stock.txt" FOR READ
OUTPUT "Report for Supplier: " & Supp
OUTPUT ""
OUTPUT "Item Description"
OUTPUT ""
WHILE NOT EOF("Stock.txt")
READFILE "Stock.txt", ThisLine
ThisCode ← MID(ThisLine, 5, 3)
IF ThisCode = Supp THEN
ThisItemNum ← LEFT(ThisLine, 4)
ThisDesc ← RIGHT(ThisLine, LENGTH(ThisLine) - 7)
OUTPUT ThisItemNum & " " & ThisDesc
Count ← Count + 1
ENDIF
ENDWHILE
CLOSEFILE "Stock.txt"
OUTPUT ""
OUTPUT "Number of items listed: ", Count
ENDPROCEDUREOutput the report header, including the supplier code parameter [1]
Conditional loop until EOF("Stock.txt") [1]
Read a line and extract the SupplierCode inside the loop [1]
If SupplierCode = Supp, increase Count (set to 0 first) [1]
Extract and output the item number and description inside the loop [1]
Output the final line with the count [1]
The count must be worked out before any item is output, so it is needed at the top of the report [1]
This means the file has to be read twice (or the lines to be output have to be stored) [1]
Loop through the file once, counting the matching items [1]
Save each selected item in an array [1]
After all lines are read, output the header lines and the count [1]
Also credited: then loop through the array to output each element (maximum 3 marks in total).
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.