SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Parameters And Overloading
Exercise:
Programming Projects
Question:13 | ISBN:9780132846813 | Edition: 5

Question

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.