7.3 - Long jump
Problem
Create a program that asks the user to enter four distances achieved in a long jump competition. The distances should be stored as an array. The program should then calculate and display the average distance jumped.
The program must match the top level design and data flow.
Example run
Enter distance(m) 1: 4.9 Enter distance(m) 2: 5.8 Enter distance(m) 3: 5.2 Average distance: 5.2 metres
| 1 | Get and store jump distances | IN | |
| OUT | distances | ||
| 2 | Calculate and display average jump distance | IN | distances |
| OUT |
1.1 Set distances to array[5] of real
1.2 for i from 0 to length of distances -1
1.3 distances[i] = get distance from user
1.4 return distances
2.1 set total to 0
2.2 for i from 0 to length of distances -1
2.3 add distances[i] to total
2.4 average = total / length of distances
2.5 display distances
# Main distances = get_distances() display_average(distances)
def get_distances():
distances = [str]*5
for i in range(len(distances)):
distances[i] = float(input("Enter distance(m) " + str(i+1) + ":"))
return distances
def display_average(distances):
total = 0
for i in range(len(distances)):
total = total + distances[i]
average = total / len(distances)
print("Average distance:", average, "metres")
# Main
distances = get_distances()
display_average(distances)
def get_distances():
distances = [str]*5
for i in range(len(distances)):
distances[i] = float(input("Enter distance(m) " + str(i+1) + ":"))
return distances # return the distances to where the function as called
def display_average(distances):
total = 0
for i in range(len(distances)):
total = total + distances[i]
average = total / len(distances)
print("Average distance:", average, "metres")
# Main
distances = get_distances() # set distances to the value returned by function
display_average(distances) # pass the distances to the procedure
Extension
Update the program with a new procedure that will find and display the longest jump.