Question 1 Report
A file scores.txt contains test scores, one per line. A program needs to create a new file passes.txt containing only the scores that are 50 or above.
(a) Write pseudocode for this filtering program. [5]
(b) Write pseudocode to count the number of passes and fails and append a summary line to passes.txt. [3]
(c) Explain why two different files are used rather than modifying the original file. [2]
(a) The filtering program reads each score from the input file, checks whether it meets the pass threshold (50 or above), and writes only passing scores to the output file. [5]
DECLARE Line : STRING
DECLARE Score : INTEGER
OPENFILE "scores.txt" FOR READ
OPENFILE "passes.txt" FOR WRITE
WHILE NOT EOF("scores.txt") DO
READFILE "scores.txt", Line
Score ← STR_TO_NUM(Line)
IF Score >= 50 THEN
WRITEFILE "passes.txt", Line
ENDIF
ENDWHILE
CLOSEFILE "scores.txt"
CLOSEFILE "passes.txt"The algorithm works in these stages:
(b) The counting extension adds two counters and appends a summary line to the passes file. [3]
DECLARE PassCount : INTEGER
DECLARE FailCount : INTEGER
DECLARE Summary : STRING
PassCount ← 0
FailCount ← 0
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF("scores.txt") DO
READFILE "scores.txt", Line
Score ← STR_TO_NUM(Line)
IF Score >= 50 THEN
PassCount ← PassCount + 1
ELSE
FailCount ← FailCount + 1
ENDIF
ENDWHILE
CLOSEFILE "scores.txt"
Summary ← "Passes: " & NUM_TO_STR(PassCount) & " Fails: " & NUM_TO_STR(FailCount)
OPENFILE "passes.txt" FOR APPEND
WRITEFILE "passes.txt", Summary
CLOSEFILE "passes.txt"The counting code reads through scores.txt a second time, incrementing PassCount for scores >= 50 and FailCount for scores below 50. After counting, the summary string is built using concatenation (&), with NUM_TO_STR converting the integer counts to strings. The passes.txt file is opened FOR APPEND (not FOR WRITE) so the existing pass scores are preserved and the summary is added at the end.
(c) Using two separate files rather than modifying the original provides two benefits. [2]
Everything you need to excel in your exams