A Class Average That Counts Absent Students
An Intermediate computer student writes the following pseudocode to process the marks of 40 students in a class:
total = 0 pass = 0 for i = 1 to 40 input mark total = total + mark if mark > 33 then pass = pass + 1 end if next i average = total / 40 print "Average =", average print "Passed =", pass
The teacher enters 90, 80, 33, and -1 for a student who was absent; the program prints an average lower than expected and counts the student with 33 marks as failed. The school's pass mark is 33, and a student with exactly 33 passes. The teacher also wants absent students excluded from the average, not counted as zero or -1. The class actually has 38 students present on the test day, although 40 are enrolled.
1. Identify the errors in the pseudocode: the pass condition, the average denominator, and the handling of absent students. Explain each using the given data.
Read the worked answer
First, if mark > 33 excludes exactly 33, but the rule says 33 passes; it must be >= 33. Second, average = total / 40 divides by enrolled students, not by students present. With 90, 80, 33, and -1, the total is 202, so the program prints 202/40 = 5.05, which is meaningless as a class average. Third, the absent student is treated as a mark: -1 is added to the total and also counted in the denominator. The correct present-only total is 90 + 80 + 33 = 203 over 3 students, giving 67.67. The absent student should be detected and skipped before adding to the total or the present count. A further issue is that absent students should not be counted as failures either; the pass count and the present count are separate concepts.
2. Rewrite the pseudocode with a present counter, the correct pass condition, and a guard against division by zero.
Read the worked answer
total = 0, pass = 0, present = 0. For i = 1 to 40: input mark. If mark = -1 then skip the rest of the loop body (or wrap the remaining statements in if mark <> -1 then). Otherwise set present = present + 1, total = total + mark, and if mark >= 33 then pass = pass + 1. After the loop, if present = 0 then print “No students present” and do not divide; otherwise average = total / present and print average, pass, and present. Optionally validate that mark is between 0 and 100 before processing. This structure separates data validation, absent handling, and calculation. The zero-present guard matters because a class could be absent on a test day, and dividing by zero would crash the program or produce an undefined result.
3. Dry-run the corrected logic on 90, 80, 33, and -1. State the output and give one additional test case that checks an edge condition.
Read the worked answer
Processing 90: present = 1, total = 90, pass = 1. Processing 80: present = 2, total = 170, pass = 2. Processing 33: present = 3, total = 203, pass = 3 because 33 >= 33. Processing -1: skipped, so present stays 3 and total stays 203. Average = 203/3 = 67.666…, displayed as 67.67 to two decimal places or 68 if the program rounds to an integer. The original logic would print 202/4 = 50.5 and pass = 2, showing both the denominator and the condition errors. A useful edge test is four -1 entries: present = 0, so the guard prints “No students present” instead of dividing by zero. Another edge test is marks 100 and 0, confirming that both valid extremes are accepted and that 0 is not confused with absent.