9618/21/M/J/21 question 3: worked solution (May/June 2021, 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. Q43 9618/21/M/J/21 original Q3 Structured · 10 marks Worked solution (a)(i) Think about what is lost when the computer is switched off: the data in the arrays. Write: Before the program ends (at the end of the day), the data in the arrays is written to text files [1] At the start of the next day the data is read from the files back into the arrays , so it does not have to be typed in again [1] (a)(ii) Write: the data in a text file is kept when the computer is switched off, because the file is non-volatile (permanent) storage [1] (a)(iii) Join the three items into one string and store it as one line of the file. Then separate the items: Join the name, number of people and event type into one string, stored as a single line in the file [1] Put a special separator character between the items (or give each item a fixed length) so they can be split apart again [1] Another accepted way: turn the number of people into a string and store each item on its own line, one after the other. (b) Open the file, check if it is empty, then read and output up to five lines with a conditional loop: PROCEDURE Preview(ThisFile : STRING) DECLARE LineNum : INTEGER DECLARE ThisLine : STRING OPENFILE ThisFile FOR READ IF EOF(ThisFile) THEN OUTPUT "Warning: the file is empty" ELSE LineNum ← 1 WHILE LineNum < 6 AND NOT EOF(ThisFile) READFILE ThisFile, ThisLine OUTPUT ThisLine LineNum ← LineNum + 1 ENDWHILE ENDIF CLOSEFILE ThisFile ENDPROCEDURE Procedure heading with the file name parameter, and ENDPROCEDURE [1] File opened for reading and closed afterwards [1] Checks if the file is empty and outputs a warning message if it is [1] Conditional loop that stops after five lines or at the end of the file [1] Inside the loop, outputs the line and reads the next one [1] KEY IDEA Text files keep data permanently, so write the arrays to a file at the end of the day and read them back at the start. TOTAL 10 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.