HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…
HardComputer ScienceHigh School (9–10)Computer Science › ProgrammingcodingIGCSE

A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…

StemPy LabContributed by StemPy Lab

Try it on paper first. A hint, the key idea and the worked solution are here when you need them.

A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a line. Write a program that:

  • reads the 5 names from the file into an array;
  • inputs a test mark for each student into a second array, rejecting any mark below 0 or above 100 with a message;
  • finds the highest mark and the name of the student who got it;
  • writes the name of every student with a mark of 50 or more into a new file Passed.txt;
  • outputs the highest mark, its student's name and the number of students who passed.

Use constants for the number of students and the pass mark. Then dry-run it for the names Ola, Ben, Chen, Dev and Eva and the typed marks 64, 105, 48, 50, 91 and 37.

Check your answer

Work it out on paper, then type your number. The answer itself stays hidden.

Need a reminder? The key ideaShowHide

OPENFILE <name> FOR READ or FOR WRITE (WRITE makes a new file and loses the old data), then READFILE <name>, <variable> to read the next line, or WRITEFILE <name>, <value> to write one, then CLOSEFILE <name>. Files keep data after the program stops.

The worked solution

Have you tried it?

Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.

A model answer in pseudocode.

pseudocode
CONSTANT Students ← 5
CONSTANT PassMark ← 50
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE Marks : ARRAY[1:5] OF INTEGER
DECLARE Index : INTEGER
DECLARE Mark : INTEGER
DECLARE Best : INTEGER
DECLARE Passed : INTEGER
// Read the five names from a file, one a line
OPENFILE "Names.txt" FOR READ
FOR Index ← 1 TO Students
    READFILE "Names.txt", Names[Index]
NEXT Index
CLOSEFILE "Names.txt"
// Input each mark, in range 0 to 100
FOR Index ← 1 TO Students
    REPEAT
        OUTPUT "Mark for ", Names[Index]
        INPUT Mark
        IF Mark < 0 OR Mark > 100
          THEN
            OUTPUT "Mark out of range"
        ENDIF
    UNTIL Mark >= 0 AND Mark <= 100
    Marks[Index] ← Mark
NEXT Index
// The position of the highest mark
Best ← 1
FOR Index ← 2 TO Students
    IF Marks[Index] > Marks[Best]
      THEN
        Best ← Index
    ENDIF
NEXT Index
// Write the names of those who passed, and count them
Passed ← 0
OPENFILE "Passed.txt" FOR WRITE
FOR Index ← 1 TO Students
    IF Marks[Index] >= PassMark
      THEN
        WRITEFILE "Passed.txt", Names[Index]
        Passed ← Passed + 1
    ENDIF
NEXT Index
CLOSEFILE "Passed.txt"
OUTPUT "Highest mark ", Marks[Best], " by ", Names[Best]
OUTPUT "Number who passed ", Passed

The dry run. 105 is rejected, so Ben is asked again and gets 48. The marks are Ola 64, Ben 48, Chen 50, Dev 91 and Eva 37. The highest is 91, by Dev. Ola, Chen and Dev have 50 or more, so Passed.txt holds those three names and the count is 3. Chen's 50 counts, because the test is >=.

How such an answer is marked. One mark for each point, up to 15:

  1. Constants for the number of students and the pass mark.
  2. Two arrays of the right size and type.
  3. The names file opened for reading and closed.
  4. A loop that reads each name into the array.
  5. A prompt and an input of each mark.
  6. A validation loop with the range 0 to 100.
  7. An error message for a rejected mark.
  8. Each accepted mark stored in the marks array.
  9. The highest mark starting from the first element.
  10. Its position or name kept when a larger mark is found.
  11. The pass file opened for writing and closed.
  12. The names with 50 or more written to the file.
  13. A count of the students who passed.
  14. The three results output with messages.
  15. Meaningful identifiers and comments.

Watch out. Opening Passed.txt inside the loop would make a new file each time, and only the last name would be left.

Log in to save this for later
← All Problems
← Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…A school library keeps its books in a table called Books… →
More onComputer Science › Programming

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Reading and writing filesArrays and Files in Pseudocode

More on this topic

  1. Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…Medium
  2. The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…Easy
  3. The nine squares of a board are input row by row: X, O, O, then X, X, X, then O, a dot for an empty…Hard
Three teams play three games each. Points[Team, Game] holds the points of a team in a game…Medium
  • A small theatre has 5 rows of 8 seats. The program marks booked seats…Easy
  • All problems on this topic →