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) W...

Assessment: Computer Science 0478 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science - 0478

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]

Answer Details

(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:

  1. Opening files: The source file "scores.txt" is opened FOR READ (input only), and the destination file "passes.txt" is opened FOR WRITE (output only, creating a new file or overwriting an existing one).
  2. Reading loop: The WHILE NOT EOF loop reads one line at a time until the end of the file is reached. EOF (End Of File) returns TRUE when there are no more lines to read.
  3. Conversion and filtering: Each line (read as a string) is converted to an integer using STR_TO_NUM. The IF condition checks whether the score is 50 or above. Only scores meeting this threshold are written to the output file.
  4. Closing files: Both files must be closed after processing to release system resources and ensure all data is written to disk.

(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]

  1. Text files are sequential-access: they do not support removing or modifying individual lines in place. To remove failing scores, the program would need to read the entire file, skip the unwanted records, and rewrite the rest, which is effectively the same as writing to a new file. Attempting to remove bytes from the middle of a file leaves gaps or corrupts the data.
  2. Keeping the original file intact provides a safety backup. If the program crashes during processing, the original data in scores.txt is unaffected. If only one file were used and a crash occurred mid-write, both the original and filtered data could be lost or corrupted.

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning