Need Help with Code Problem

Hi, I'm trying to work on the following code problem (from Absolute c++ 6th edition book)and was very unsure how to start and write this code. I was hoping if someone could maybe give any hints or anything to get me started. I'm currently working on it and will post my progress soon as I'm still trying. Any help will be greatly appreciated as I don't completely understand what really an overloaded function is and how to code it. Thanks.

You would like to know how fast you can run in miles per hour, however your treadmill tells you your speed in terms of 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 approx. 1.61 kilometers. [Your submitted code for this question should include only the two definitions of the overloaded function, nothing else.]

I don't completely understand what really an overloaded function
Overloaded function is a set of functions with same name, but different parameters:

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
double convertToMPH(int x, int y);
The second definition should take as input one double that represents the speed in kph and return the speed in mph as a double.
double convertToMPH(double z);

(change names of parameters to something meaningful in both cases)
Hi, I tried working on the code, but I don't know what to do next even after reading the chapter from the textbook. Could someone please help me out? I don't no what to put for the brackets of the return functions where I put a ? mark. I don't no if I'm doing it right. Thanks.

#include <iostream>
using namespace std;

double convertToMPH(int x, int y);
//represents pace in minutes and seconds per mile and returns the speed in mph
double convertToMPH(double z);
//represents in kph and returns the speed in mph

//1 mile = 1.61 km
//1 km = 1000m
//d=vt, v=d/v

int main()
{

cout << "The speed in mph is " << convertToMPH(int x, int y) << endl;


cout << "The speed in mph is " << convertToMPH(double z) << endl;

return 0;
}

double convertToMPH(int x, int y)
{
return(?)
}
double convertToMPH(double z)
{
return(?)
}
So give it something to convert.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

double convertToMPH(int x, int y);
//represents pace in minutes and seconds per mile and returns the speed in mph 
double convertToMPH(double z);
//represents in kph and returns the speed in mph

//1 mile = 1.61 km
//1 km = 1000m
//d=vt, v=d/v

int main()
{

// We may want to declare some variables here if we'll be working with them.

// How do we get the values of x and y to send to the function?

cout << "The speed in mph is " << convertToMPH(int x, int y) << endl;


// What is the value of z? Again... How do we get those values to send to the function?
cout << "The speed in mph is " << convertToMPH(double z) << endl;

return 0;
}

double convertToMPH(int x, int y)
{
 // Here you could create a variable to calculate x and y into MPH and return that variable
// Or you could just return an expression (which is the formula itself)

return(return a variable or an expression)
}


double convertToMPH(double z)
{
 // Here you could create a variable to calculate z  into MPH and return that variable
// Or you could just return an expression (which is the formula itself)

return(return a variable or an expression)
}


and just as a side note... Why are your comments in KM when both of your functions return MPH ?
Last edited on
Topic archived. No new replies allowed.