3.1 Test scores

You have an array of test scores. Find and display the highest score and the lowest score. Start by using the first score as both the current maximum and current minimum, then check the rest of the array.

scores = [12, 18, 15, 20, 9, 14]
Highest score: 20
Lowest score: 9
set scores to [12, 18, 15, 20, 9, 14]

set maximum to first score in array
set minimum to first score in array

loop through scores starting at position 1
    if score at current position is greater than maximum
        set maximum to score at current position
    if score at current position is less than minimum
        set minimum to score at current position

print "Highest score:", maximum
print "Lowest score:", minimum

 

scores = [12, 18, 15, 20, 9, 14]

maximum = scores[0]
minimum = scores[0]

for i in range(1, len(scores)):
    if scores[i] > maximum:
        maximum = scores[i]
    if scores[i] < minimum:
        minimum = scores[i]

print("Highest score:", maximum)
print("Lowest score:", minimum)
# Array storing test scores
scores = [12, 18, 15, 20, 9, 14]

# Start by assuming the first score is both the max and the min
maximum = scores[0]
minimum = scores[0]

# Check from position 1 because position 0 is already used
for i in range(1, len(scores)):
    # If the current score is higher than the stored maximum, update it
    if scores[i] > maximum:
        maximum = scores[i]
    # If the current score is lower than the stored minimum, update it
    if scores[i] < minimum:
        minimum = scores[i]

# Output the results
print("Highest score:", maximum)
print("Lowest score:", minimum)

Extension

Update the program so that, as well as displaying the highest and lowest scores, it also displays the position (index) in the array where each one occurs.

Remember, arrays start at position 0. If there are ties, the first occurrence should be shown.

Highest score: 20 at position 3
Lowest score: 9 at position 4