Example - Functions
Programming project #6 from Chapter 3 of the book
Problem
Four track stars have entered the mile race at the Penn Relays.
Write a program that scans in the race time in minutes (minutes) and seconds (seconds) for a runner, and computes and displays the speed in feet per second (fps) and meters per second (mps).
Write and call a function that displays instructions to the user.
Run the program for each star's data.
| Minutes | Seconds |
| 3 | 52.83 |
| 3 | 59.83 |
| 4 | 00.03 |
| 4 | 16.22 |
Hint: There are 5,280 feet in one mile, and one kilometer equals 3,282 feet.
Analysis
Structure Chart
Inputs:
- Time in minutes = get from user
- Time in seconds = get from user
- Distance of race = 1 mile
Outputs:
- Speed in feet per second
- Speed in meters per second
Formulas:
- 1 mile = 5280 feet
- 1 km = 3282 feet
- 1 km = 1000 m
Design
Algorithm:
- Get the input from the user
- Convert input time to common unit (seconds)
- Calculate FPS
- Calculate number of feet in race distance
- Divide race distance in feet by time in seconds to get feet per second
- Calculate MPS
- Convert race distance to meters
- Convert miles to feet
- Convert feet to kms
- Convert kms to m
- Divide race distance in meters by time in seconds to get meters per second
- Print out both results
NOTE: this algorithm is for one racer only. It is to be applied 4 times, once for each racer.
Implementation
Download
Back
Last updated: 9:44pm, January 22nd, 2017