Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 2 of 3.
Plan: (1) input and validate every score with a loop inside a loop, adding to each competitor's total as you go; (2) find the highest score in each event, then output the names who match it; (3) find the highest total, then output the names who match it. Comments explain each step.
// Set every total to 0 before adding scores
FOR Competitor ← 1 TO 25
Points[Competitor] ← 0
NEXT Competitor
// Input and validate scores for every competitor in every event
FOR Event ← 1 TO 5
FOR Competitor ← 1 TO 25
REPEAT
OUTPUT "Enter score (0 to 100) for ", CompetitorName[Competitor], " in event ", Event
INPUT Score
UNTIL Score >= 0 AND Score <= 100 // reject invalid scores
CompetitorScore[Competitor, Event] ← Score
// add to this competitor's total
Points[Competitor] ← Points[Competitor] + Score
NEXT Competitor
NEXT Event
// Find and output the medal winners for each event
FOR Event ← 1 TO 5
HighestScore ← 0
// find the highest score in this event
FOR Competitor ← 1 TO 25
IF CompetitorScore[Competitor, Event] > HighestScore
THEN
HighestScore ← CompetitorScore[Competitor, Event]
ENDIF
NEXT Competitor
OUTPUT "Medal winners for the highest score in event ", Event
// output everyone who matches the highest score (there may be ties)
FOR Competitor ← 1 TO 25
IF CompetitorScore[Competitor, Event] = HighestScore
THEN
OUTPUT CompetitorName[Competitor]
ENDIF
NEXT Competitor
NEXT Event
// Find the highest total points for the five events
HighestTotal ← 0
FOR Competitor ← 1 TO 25
IF Points[Competitor] > HighestTotal
THEN
HighestTotal ← Points[Competitor]
ENDIF
NEXT Competitor
// Output the competitors with the highest total
OUTPUT "Competitors with the highest total points:"
FOR Competitor ← 1 TO 25
IF Points[Competitor] = HighestTotal
THEN
OUTPUT CompetitorName[Competitor]
ENDIF
NEXT CompetitorProgramming skills (up to 9 marks):
Nested loops, input with a prompt and validation of 0 to 100 for every competitor and event [3]
Highest score for each event found with selection and the names with that score output, with a message [3]
Totals worked out, highest total found and the names with it output, with a message [3]
Quality of the solution (up to 6 marks):
Comments that explain how the code works, all the way through [2]
Meaningful identifier names, and the given arrays CompetitorName, CompetitorScore and Points used [2]
Logical order and accurate code that meets all the requirements (minor syntax errors are ignored) [2]
Any other correct way also scores, for example working out the totals in a separate loop or using a WHILE loop for validation.
▶ Topic lesson
A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.