Calling a function hw help.

Hey guys I'm attempting to call a function to calculate Feet per Second and Meters per second.

Here is the question: Four track stars entered the mile race at the Penn Relays. Write a program that will
read the last name and the race time in minutes and seconds for one runner and
compute and print the speed in feet per second and in meters per second after the
runner’s name. (hints: There are 5,280 feet in one mile, and one kilometer equals 3281
feet; one meter is equal to 3.281 feet.) Write and call a function that displays instructions to the program user. Write two other
functions, one to compute the speed in meters per second and one to compute the speed in feet
per second.

Here is what I have come up with so far. I had no trouble calling the void function for instructions but I'm struggling to come up with a proper way to call the functions to calculate the 2 values.


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
45
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

  void instructions()
{

	cout << "For this program you will be prompted to input a runner's last name, minutes,   and seconds." << endl;
	cout << "The program will then calculate and output the runner's speed in Feet/Second andMeters/Second." << endl;
	cout << endl;

}


int main()
{
	instructions ();
	
	string runner;
	int minutes;
	float seconds, fps, mps, total;		//fps = Feet per second. mps = Meters per second. total = total seconds calculated.

	cout << "Please enter the Last Name of the runner: " << endl;
	cin >> runner;
	cout << endl;

	cout << "Please enter the runners' time in Minutes: " << endl;
	cin >> minutes;
	cout << endl;

	cout << "Please enter the runners' time in Seconds: " << endl;
	cin >> seconds;
	cout << endl;

	total = (minutes * 60) + seconds;		//Takes minutes from user input, multiplies it by 60 for seconds in a minute, and then adds user inputted seconds for a total number of seconds
	fps = 5280 / total;						//Feet in a mile divided by the total seconds calculated above
	mps = fps * 0.3048;						//Calculated Feet per second multipled by the number of meters per foot (0.3048)

	cout << runner << " ran a rate of " << fps << " feet per second or " << mps << " meters per second." << endl;


	return 0;
}
Last edited on
You can crate two functions of type double and return the value after calculations.
Last edited on
float seconds, fps, mps, total;
There is rarely a reason to use float over double, especially when doing calculations involving floating point numbers. I'd recommend you change the type to double.
Also, as floating point numbers aren't represented exactly, it's always good to use integral types (int, unsigned, short, etc...) for each time denomination; a variable for hours, a variable for minutes, a variable for seconds, etc.

1
2
fps = 5280 / total;
mps = fps * 0.3048;

You're assuming that the runner runs a distance of a mile. You haven't asked the user for the distance. Also, what do the magic numbers 5280 and 0.3048 mean? Whenever you have these magic numbers, it's a good practice to name them and make them constants, to improve readability of code. For example,
 
const double fps_to_mps = 0.3048;


As for turning your conversions to functions
1
2
3
4
double calculate_fps( double mi, int sec )
{
    // code here
}
I attempted to input the conversion you suggested and it's not working out how I envisioned.
I attempted to input the conversion you suggested and it's not working out how I envisioned.

For example?
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

void instructions()
{

	cout << "For this program you will be prompted to input a runner's last name, minutes,   and seconds." << endl;
	cout << "The program will then calculate and output the runner's speed in Feet/Second andMeters/Second." << endl;
	cout << endl;

}

double calculate_fps(double minutes, double seconds) 
{
	double fps;
	double total;
	total = (minutes * 60) + seconds;
	fps = 5280 / total;
	return (fps);

}

double calculate_mps(double fps, double = 0.3048)
{

	double mps;
	mps = fps * 0.3048;
	return (mps);
}

int main()
{
	instructions ();
	
	string runner;
	int minutes;
	double seconds, fps, mps, total;		//fps = Feet per second. mps = Meters per second. total = total seconds calculated.

	cout << "Please enter the Last Name of the runner: " << endl;
	cin >> runner;
	cout << endl;

	cout << "Please enter the runners' time in Minutes: " << endl;
	cin >> minutes;
	cout << endl;

	cout << "Please enter the runners' time in Seconds: " << endl;
	cin >> seconds;
	cout << endl;

	total = (minutes * 60) + seconds;		//Takes minutes from user input, multiplies it by 60 for seconds in a minute, and then adds user inputted seconds for a total number of seconds
	fps = 5280 / total;						//Feet in a mile divided by the total seconds calculated above
	mps = fps * 0.3048;						//Calculated Feet per second multipled by the number of meters per foot (0.3048)

	cout << runner << " ran a rate of " << calculate_fps << " feet per second or " << calculate_mps << " meters per second." << endl;


	return 0;
}
 
fps = 5280 / total;


Also, what do the magic numbers 5280 and 0.3048 mean? Whenever you have these magic numbers, it's a good practice to name them and make them constants, to improve readability of code. For example,
1
2
	
const double fps_to_mps = 0.3048;



double calculate_fps(double minutes, double seconds)
speed = distance / time
How do you calculate the speed if you don't have a distance?

double calculate_mps(double fps, double = 0.3048)
Not sure what you're trying to achieve here.

You also don't call the functions in main().
The distance is a mile. It's stated in the question that I listed in my OP.

Four track stars entered the mile race at the Penn Relays.


I appreciate your help but your vague responses have not been explaining much.

I knew how to call void functions I'm just unaware of how to call mathematical functions and where their place within the program is.

I guess what I'm saying is, I personally work better off of an example work which is unavailable at this time.
Last edited on
Here's where I am now. My outputs for Feet per Second and meters per second are not properly working. Could be my algorithm?


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

void instructions();
double calculate_fps(double minutes, double seconds);
double calculate_mps(double fps, double feet_meter);

int main()
{

	string runner;
	int minutes;
	double seconds, fps, mps, total;			//Feet per second, meters per second, total seconds
	const double fpM = 0.3408;					//Feet per Meter	
	double result;								//Feet per second result
	double meterResult;							//Meter per second result


	instructions();

	cout << "Please enter the Last Name of the runner: " << endl;
	cin >> runner;
	cout << endl;

	cout << "Please enter the runners' time in Minutes: " << endl;
	cin >> minutes;
	cout << endl;

	cout << "Please enter the runners' time in Seconds: " << endl;
	cin >> seconds;
	cout << endl;

	result = calculate_fps(minutes, seconds);

	total = (minutes * 60) + seconds;	

	meterResult = calculate_mps(result, fpM);					
																

	cout << runner << " ran a rate of " << calculate_fps << " feet per second or " << calculate_mps << " meters per second." << endl;


	return 0;
}

void instructions()
{
	cout << endl;
	cout << "For this program you will be prompted to input a runner's last name, minutes,   and seconds." << endl;
	cout << "The program will then calculate and output the runner's speed in Feet/Second andMeters/Second." << endl;
	cout << endl;

}

double calculate_fps(double minutes, double seconds)
{
	double fps;
	double total;
	const int mile = 5280;
	total = (minutes * 60) + seconds;
	fps = mile / total;
	return (fps);

}

double calculate_mps(double fps, double feet_meter)
{

	double mps;
	mps = fps * 0.3048;
	return (mps);
}


NVM I figured it out.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

void instructions();
double calculate_fps(double minutes, double seconds);
double calculate_mps(double fps, double feet_meter);

int main()
{

	string runner;
	int minutes;
	double seconds, fps, mps, total;			//Feet per second, meters per second, total seconds
	const double fpM = 0.3408;					//Feet per Meter	
	double result;								//Feet per second result
	double meterResult;							//Meter per second result


	instructions();

	cout << "Please enter the Last Name of the runner: " << endl;
	cin >> runner;
	cout << endl;

	cout << "Please enter the runners' time in Minutes: " << endl;
	cin >> minutes;
	cout << endl;

	cout << "Please enter the runners' time in Seconds: " << endl;
	cin >> seconds;
	cout << endl;

	result = calculate_fps(minutes, seconds);

	total = (minutes * 60) + seconds;	

	meterResult = calculate_mps(result, fpM);					
																

	cout << runner << " ran a rate of " << result << " feet per second or " << meterResult << " meters per second." << endl;


	return 0;
}

void instructions()
{
	cout << endl;
	cout << "For this program you will be prompted to input a runner's last name, minutes,   and seconds." << endl;
	cout << "The program will then calculate and output the runner's speed in Feet/Second andMeters/Second." << endl;
	cout << endl;

}

double calculate_fps(double minutes, double seconds)
{
	double fps;
	double total;
	const int mile = 5280;
	total = (minutes * 60) + seconds;
	fps = mile / total;
	return (fps);

}

double calculate_mps(double fps, double feet_meter)
{

	double mps;
	mps = fps * 0.3048;
	return (mps);
}
Topic archived. No new replies allowed.