An algorithm will output the last three lines from a text file Result.txt The lines need to be output in the same order as they appear in the file. Assume: • Three variables LineX, LineY and LineZ will store the three lines. These are of type string and all three variables have been initialised to an empty string. • The file exists and contains at least three lines.
- (a)The algorithm to output the lines is expressed in eight steps. Complete the steps. 1. Open the file 2. Loop until 3. 4. Assign LineY to LineX 5. Assign LineZ to LineY 6. Assign ThisLine to LineZ 7. After the loop, 8. Output LineX, LineY, LineZ and store in ThisLine[4]
- (b)Explain the purpose of steps 4, 5 and 6 in the algorithm from part (a).[1]
- (c)The requirement changes, and the algorithm will now output three lines from the file, starting from a given line number. The modified algorithm will be implemented as a function which will: • be called with an integer parameter representing the given line number • output three lines, starting at the given line • return TRUE if the 3 lines are output, or FALSE if it was not possible to output the 3 lines. Describe the changes that need to be made to steps 2 to 8 of the algorithm given in part (a).[4]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.
- [1] (a) 1. Open the file (Result.txt) in read mode
- [1] (a) 2. Loop until EOF(Result.txt) // EOF // end of file
- [1] (a) 3. Read a / the / next line (from the file) and store in ThisLine
- [1] (a) 4. Assign LineY to LineX
- [1] (a) 5. Assign LineZ to LineY
- [1] (a) 6. Assign ThisLine to LineZ
- [1] (a) 7. After the loop, close the file (Result.txt)
- [1] (a) 8. Output LineX, LineY, LineZ
- [1] (b) So that the last three lines of the file are output in the correct order
- [1] (b) (Ensuring) the lines are output in correct order
- [1] (b) Ordering the last three lines
- [1] (b) Ordering the lines so they are in the same order as (they occur) in the file
- [1] (c) Loop until given line number (-1)/ <parameter> (-1) (lines have been read)
- [1] (c) if end of file is reached then return FALSE
- [1] (c) Loop for three lines // Read three lines // Repeat of 4 and 5 for three lines
- [1] (c) Read a line and output it
- [1] (c) if end of file is reached then return FALSE
- [1] (c) After outputting the (required) lines return TRUE
- [1] (c) Ordering of lines no longer needed
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →