Running Times
A program is required to track a person’s running times.
Write a program that stores a person’s 5km running times. A 1-D Array is required to store each running time in minutes. The program should then loop through the array and add all times together to create the ‘total time’. The total time should then be used to calculate the average 5k time, as well as the average time to run 1km. At the end of the program, each time should be displayed to the user as well as the total running time, average, and 1km times.
Race 1 : 34 minutes Race 2 : 36 minutes Race 3 : 35 minutes Race 4 : 37 minutes Total time: 142 minutes Average 5k time: 35.5 minutes Average 1k time: 7.1 minutes
DECLARE times INITIALLY [34, 36, 35, 37]
DECLARE total INITIALLY 0
DECLARE average5k INITIALLY 0
DECLARE average1k INITIALLY 0
FOR i FROM 0 TO LENGTH(times) - 1 DO
total = total + times[i]
END FOR
SET average5k TO total / LENGTH(times)
SET average1k TO average5k / 5
FOR i FROM 0 TO LENGTH(times) - 1 DO
SEND "Race " & (i + 1) & ": " & times[i] & " minutes" TO DISPLAY
END FOR
SEND "Total time: " & total & " minutes" TO DISPLAY
SEND "Average 5k time: " & average5k & " minutes" TO DISPLAY
SEND "Average 1k time: " & average1k & " minutes" TO DISPLAY
times = [34, 36, 35, 37] # in minutes
total = 0
for i in range(len(times)):
total = total + times[i]
average_5k = total / len(times)
average_1k = average_5k / 5
for i in range(len(times)):
print("Race ", i+1, ": ", times[i], " minutes")
print("Total time:", total, "minutes")
print("Average 5k time:", average_5k, "minutes")
print("Average 1k time:", average_1k, "minutes")
# Times of each 5k run in minutes
times = [34, 36, 35, 37]
# Set the total to 0
total = 0
# Loop through each running time
for i in range(len(times)):
# Increase the total by each time
total = total + times[i]
# Calculate the average 5k times by dividing the total by the number of times in array
average_5k = total / len(times)
# Calculate the average 1k time by dividing the average 1k time by 5
average_1k = average_5k / 5
# Loop through each running time
for i in range(len(times)):
# Display the time of each race for the user
print("Race ", i+1, ": ", times[i], " minutes")
# Display each of the running metrics that were calculated
print("Total time:", total, "minutes")
print("Average 5k time:", average_5k, "minutes")
print("Average 1k time:", average_1k, "minutes")
Extension
Exten the program by implementing the following:
- Add another 5 running times to the array
- Calculate how many running times were less than the average 5k
- Round all averages to 1 decimal place
Race 1 : 34 minutes Race 2 : 36 minutes Race 3 : 35 minutes Race 4 : 37 minutes Race 5 : 38 minutes Race 6 : 37 minutes Race 7 : 35 minutes Race 8 : 34 minutes Race 9 : 35 minutes Total time: 321 minutes Average 5k time: 35.7 minutes Average 1k time: 7.1 minutes There were 5 running times faster than the average of 35.7 minutes .