The following pseudocode represents an algorithm intended to output the last three lines as they appear in a text file. Line numbers are provided for reference only. ```pseudocode 10 PROCEDURE LastLines(ThisFile : STRING) 11 DECLARE ThisLine : STRING 12 DECLARE Buffer : ARRAY[1:3] OF STRING 13 DECLARE LineNum : INTEGER 14 LineNum ← 1 15 OPENFILE ThisFile FOR READ 16 17 WHILE NOT EOF(ThisFile) 18 READFILE Thisfile, ThisLine // read a line 19 Buffer[LineNum] ← ThisLine 20 LineNum ← LineNum + 1 21 IF LineNum = 4 THEN 22 LineNum ← 1 23 ENDIF 24 ENDWHILE 25 26 CLOSEFILE ThisFile 27 FOR LineNum ← 1 TO 3 28 OUTPUT Buffer[LineNum] 29 NEXT LineNum 30 ENDPROCEDURE ```
- (a)(i)There is an error in the algorithm. In certain cases, a text file will have at least three lines but the output will be incorrect. State how the output may be incorrect.[1]
- (a)(ii)Describe the error in the algorithm and explain how it may be corrected. Description Explanation[4]
- (b)The original algorithm is implemented and sometimes the last three lines of the text file are output correctly. State the condition that results in the correct output.[1]
- (c)Lines 20 to 23 inclusive could be replaced with a single pseudocode statement. Write the pseudocode statement.[2]
Mark scheme
Marking points from the official mark scheme (May/June 2022). The mark or mark type is shown in brackets.
- [1] (a)(i) The lines are output in an incorrect sequence / in the wrong order / not as they appear in the file
- [4] (a)(ii) Description of error: If the final line of the file is not written into array element 3, then outputting the elements in the sequence 1 to 3 will give the error.
- [4] (a)(ii) Explanation of error correction (Option 1): Attempt at description of 'shuffle' / Copy Buffer[2] to Buffer[1] AND copy Buffer[3] to Buffer[2] / Read a line from the file and write it to Buffer[3]
- [4] (a)(ii) Explanation of error correction (Option 2): Store the index of the last element written to buffer (the last line of the file) / Replace the FOR loop with something that starts at index and then wraps around (MOD 3)
- [4] (a)(ii) Explanation of error correction (Option 3 - two-loop solution, not using an array): Loop to read file to end to get number of lines / close and re-open file / read (and discard) lines to number of lines - 3, then loop to read and output last 3 lines
- [1] (b) If the number of lines in the text file is a multiple of three
- [2] (c) Correct answers include: LineNum ← (LineNum MOD 3) + 1 // ((LineNum + 3) MOD 3) + 1
- [2] (c) One mark for assignment to LineNum making any use of MOD
- [2] (c) One for completely correct statement
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 →