3.4 Class champs
A teacher wants to know which pupils achieved the highest score in a test. You are given two parallel arrays: one stores pupil names and the other stores their test scores.
The program should:
- Find the highest score in the scores array.
- Count how many pupils achieved this score.
- Display the names of all pupils who achieved it.
This is useful because sometimes more than one pupil can be “top of the class”.
names = ["Jane", "John", "Jess", "Ava", "Sam"] scores = [18, 20, 17, 20, 15]
Highest score: 20 Number of top scorers: 2 Top scorers: John Ava
set names to ["Jane", "John", "Jess", "Ava", "Sam"]
set scores to [18, 20, 17, 20, 15]
set max_score to first value in scores
loop from position 1 to length(scores) - 1
if score at current position > max_score
set max_score to score at current position
set count to 0
loop for length of scores
if score at current position = max_score
set count to count + 1
output "Highest score: " and max_score
output "Number of top scorers: " and count
output "Top scorers:"
loop for length of scores
if score at current position = max_score
output name at current position
names = ["Jane", "John", "Jess", "Ava", "Sam"]
scores = [18, 20, 17, 20, 15]
# Step 1: Find the maximum score
max_score = scores[0]
for i in range(1, len(scores)):
if scores[i] > max_score:
max_score = scores[i]
# Step 2: Count how many pupils achieved the max
count = 0
for i in range(len(scores)):
if scores[i] == max_score:
count = count + 1
# Step 3: Display the results
print("Highest score:", max_score)
print("Number of top scorers:", count)
print("Top scorers:")
for i in range(len(scores)):
if scores[i] == max_score:
print(names[i])
# Parallel arrays: pupil names and their scores
names = ["Jane", "John", "Jess", "Ava", "Sam"]
scores = [18, 20, 17, 20, 15]
# Step 1: Find the maximum score
# Start with the first score as the current max
max_score = scores[0]
# Loop from index 1 onwards
for i in range(1, len(scores)):
if scores[i] > max_score:
max_score = scores[i]
# Step 2: Count how many pupils achieved the maximum score
count = 0
for i in range(len(scores)):
if scores[i] == max_score:
count = count + 1
# Step 3: Display the results
print("Highest score:", max_score)
print("Number of top scorers:", count)
# Step 4: Display the names of the pupils with the max score
print("Top scorers:")
for i in range(len(scores)):
if scores[i] == max_score:
print(names[i])
Extension
After finding the top scorers, update the program to also:
Work out the average score of the class.
For each pupil, display whether their score is above, below, or equal to the average.
This helps put each pupil’s performance into context, not just focusing on the top score.
Names: ["Jane", "John", "Jess", "Ava", "Sam"] Scores: [18, 20, 17, 20, 15] Highest score: 20 Number of top scorers: 2 Top scorers: John Ava Average score: 18 Jane scored 18 which is equal to the average John scored 20 which is above the average Jess scored 17 which is below the average Ava scored 20 which is above the average Sam scored 15 which is below the average