9618/22/O/N/23 question 6: worked solution (October/November 2023, paper 22)

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. Q55 9618/22/O/N/23 original Q6 Structured · 9 marks Worked solution (a) Loop for each file number, turn it into a three-character suffix by adding zeros at the front, then create the file and write one line to it. PROCEDURE CreateFiles(NameRoot : STRING, NumFiles : INTEGER) DECLARE FileName, Suffix : STRING DECLARE Count : INTEGER FOR Count ← 1 TO NumFiles Suffix ← NUM_TO_STR(Count) WHILE LENGTH(Suffix) <> 3 Suffix ← '0' & Suffix ENDWHILE FileName ← NameRoot & '.' & Suffix OPENFILE FileName FOR WRITE WRITEFILE FileName, "This is File " & FileName CLOSEFILE FileName NEXT Count ENDPROCEDURE Procedure heading with both parameters, and ending [1] Loop that runs NumFiles times [1] Attempt to make the suffix using NUM_TO_STR() inside a loop [1] Completely correct file name, for example TestData.002 [1] OPENFILE in WRITE mode and the matching CLOSEFILE , inside the loop [1] The first line written to each file inside the loop [1] (b)(i) It returns a value, so use a function . [1] (b)(ii) FUNCTION CheckFiles(NameRoot : STRING) RETURNS INTEGER Correct header with the string parameter and RETURNS INTEGER [1] (b)(iii) The files are only being checked and read, so use READ mode. [1] KEY IDEA Build file names with string functions and always open and close each file inside the loop. TOTAL 9 marks Official mark scheme VIDEO ▶ Starts at Q6 (33:35) Starts at Q6 (33:35) · by Pseudocode Pro. Tap the picture to open it on YouTube.