You would like to know how fast you can run in miles per hour. Your treadmill will tell you your speed in terms of a pace (minutes and seconds per mile, such as “5:30 mile”) or in terms of kilometers per hour (kph). Write an overloaded function called convertToMPH . The first definition should take as input two integers that represent the pace in minutes and seconds per mile and return the speed in mph as a double. The second definition should take as input one double that represents the speed in kph and return the speed in mph as a double. One mile is approximately 1.61 kilometers. Write a driver program to test your function.
Program code:
//Header section
#include <iostream>
using namespace std;
//Function definition of convertToMPH with two arguments
double convertToMPH(int minutes, int seconds)
{
// Calculate the total time in seconds per mile
int totalSeconds = minutes * 60 + seconds;
// Calculate the speed in miles per hour
double mph = 3600.0 / totalSeconds;
return mph;
}
//Function definition of convertToMPH with a single argument
double convertToMPH(double kph)
{
// Convert kilometers per hour to miles per hour
double mph = kph / 1.61;
return mph;
}
//Program starts with a main method
int main()
{
// Testing the first definition of convertToMPH
int paceMinutes = 5;
int paceSeconds = 30;
double speed1 = convertToMPH(paceMinutes, paceSeconds);
cout << "Speed in mph (pace): " << speed1 << endl;
// Testing the second definition of convertToMPH
double kphSpeed = 12.0;
double speed2 = convertToMPH(kphSpeed);
cout << "Speed in mph (kph): " << speed2 << endl;
return 0;
}
OUTPUT OF THE PROGRAM CODE:
Speed in mph (pace): 10.9091
Speed in mph (kph): 7.45342