Rollercoasters

Write a program that gets the height of 3 rollercoasters and calculates their total height and average height. The program should allow the user to enter the height of three rollercoasters. These heights should be stored within the array. Each height should also be added to a running total. After all three heights have been recorded, the average can be calculated before all the information is displayed back to the user.

Enter height of rollercoaster 1 in metres: 41.3
Enter height of rollercoaster 2 in metres: 28.7
Enter height of rollercoaster 3 in metres: 36.5
Rollercoaster 1 : 41.3 metres
Rollercoaster 2 : 28.7 metres
Rollercoaster 3 : 36.5 metres
Total height: 106.5
Average height: 35.5
DECLARE heights[3] AS REAL
DECLARE total AS REAL
DECLARE average AS REAL

SET total TO 0

FOR i FROM 0 TO 2 DO
    OUTPUT "Enter height of rollercoaster", i + 1
    INPUT heights[i]
    SET total TO total + heights[i]
END FOR

SET average TO total / 3

FOR i FROM 0 TO 2 DO
    OUTPUT "Rollercoaster", i + 1, ":", heights[i], "metres"
END FOR

OUTPUT "Total height:", total
OUTPUT "Average height:", average

heights = [float] * 3
total = 0

for i in range(3):
    heights[i] = float(input("Enter height of rollercoaster " + str(i + 1) + " in metres: "))
    total = total + heights[i]

average = total / 3

for i in range(3):
    print("Rollercoaster", str(i + 1), ":", heights[i], "metres")

print("Total height:", round(total, 2))
print("Average height:", round(average, 2))
# Create an array to store 3 rollercoaster heights
heights = [float] * 3
total = 0

# Ask user to enter each height
for i in range(3):
    # Set the position in the array to whatever the user inputs
    heights[i] = float(input("Enter height of rollercoaster " + str(i + 1) + " in metres: "))
    # Increase total by the value at this position in the array
    total = total + heights[i]

# Calculate average height
average = total / 3

# Loop through each rollercoaster
for i in range(3):
    # Display each rollercoasters height
    print("Rollercoaster", str(i + 1), ":", heights[i], "metres")

# Show total and average height
print("Total height:", total)
print("Average height:", average)

Extension

Extend the program by letting the user set how many rollercoasters are in the park. The array and loops should then use this value throughout the program.

How many rollercoasters are at this park? 5
Enter height of rollercoaster 1 in metres: 27.3
Enter height of rollercoaster 2 in metres: 19.1
Enter height of rollercoaster 3 in metres: 25.8
Enter height of rollercoaster 4 in metres: 39.0
Enter height of rollercoaster 5 in metres: 33.8
Rollercoaster 1 : 27.3 metres
Rollercoaster 2 : 19.1 metres
Rollercoaster 3 : 25.8 metres
Rollercoaster 4 : 39.0 metres
Rollercoaster 5 : 33.8 metres
Total height: 145.0
Average height: 48.33

Target

You should be able to use 1-D Arrays to solve a problem.