Question 1Write Cambridge pseudocode to copy nonempty lines from Input.txt to Clean.txt, replacing any old Clean.txt. Input contains three lines: A, an empty line, BC. State final output and number of READFILE operations; also trace an empty input file and explain why APPEND would violate the replacement requirement.6 indicative marks
Write Cambridge pseudocode to copy nonempty lines from Input.txt to Clean.txt, replacing any old Clean.txt. Input contains three lines: A, an empty line, BC. State final output and number of READFILE operations; also trace an empty input file and explain why APPEND would violate the replacement requirement.
Show indicative guidance
Worked guidance
DECLARE Line : STRING OPENFILE "Input.txt" FOR READ OPENFILE "Clean.txt" FOR WRITE WHILE NOT EOF("Input.txt") READFILE "Input.txt", Line IF Line <> "" THEN WRITEFILE "Clean.txt", Line ENDIF ENDWHILE CLOSEFILE "Input.txt" CLOSEFILE "Clean.txt"- The three reads yield A, empty, BC. Only A and BC are written, in that order. For an empty input, EOF is already TRUE, so no attempted read occurs. A post-condition loop would try an invalid first read unless separately guarded.
- WRITE creates or truncates the output, removing OLD content; APPEND would produce OLD,A,BC. READFILE returns text lines as STRING. Actual file permissions, media errors and practical testing remain part of teacher-led work.
Self-marking points
View marking points
- Open input FOR READ and output FOR WRITE.
- Declare the line variable STRING and test NOT EOF before each read.
- Read each line exactly once.
- Write only a line not equal to the empty string.
- Close both files; output lines A and BC after three reads.
- Empty input causes zero reads/writes; APPEND would preserve obsolete output lines.
Examiner tip
Indicative distinct points, not an official Cambridge mark allocation. State bounds, assumptions and intermediate states; practical teacher review remains required.