Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This is the kind of long scenario question that ends Paper 2. Answer in pseudocode.
A teacher wants a program for the results of a class test. The program must:
Write the algorithm with comments. Then give its outputs for a class of : Ana , Ben , Chen first typed as and then corrected to , and Dev .
Totalling: Total ← 0 before the loop, Total ← Total + Value inside. Counting: Count ← 0 before, Count ← Count + 1 inside (often in an IF). Maximum and minimum: start with the first value or an extreme value, and replace it with any larger (or smaller) value. Average: Total / Count after the loop.
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.
Plan. One FOR loop over the pupils; inside it a validation loop for the mark, then totalling, counting and the maximum.
A model answer.
// Class test results
INPUT Size
Total ← 0
Passes ← 0
Highest ← -1
FOR Pupil ← 1 TO Size
INPUT Name
REPEAT
INPUT Mark
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "Mark must be 0 to 100: enter it again"
ENDIF
UNTIL Mark >= 0 AND Mark <= 100
Total ← Total + Mark
IF Mark >= 50
THEN
Passes ← Passes + 1
ENDIF
IF Mark > Highest
THEN
Highest ← Mark
TopName ← Name
ENDIF
NEXT Pupil
OUTPUT "Average: ", ROUND(Total / Size, 1)
OUTPUT "Highest: ", Highest, " by ", TopName
OUTPUT "Marks of 50 or more: ", PassesThe run. The is refused with the message, and is accepted. Total , so the average is . The highest mark is , by Chen. The marks of or more are , and : pupils.
A marking list. One mark for each point.
Total starts at before the loop.Total.Passes starts at and goes up for a mark of or more (the boundary included).Highest starts low (or at the first mark).Highest.Total / Size, worked out after the loop.Answer: average , highest by Chen, and pupils with or more.
What if? If two pupils share the highest mark, this algorithm keeps the first, because it uses >. With >= it would keep the last.
Log in to join the discussion.
Loading…