Question with functions and division.

I'm trying to pick up simple coding on my own using online resources and books, I'm trying to create a code that converts the number of meters to miles, yards, and inches, accurate to the nearest inch.

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

double meter_to_yards (double meters);
void display_mi_yd_in (double meters);

int main()
{
double m

cout << "Enter number of meters. ";
cin >> m;
m = meter_to_yards (m);
display_mi_yd_in(m);

return 0;
}

double meter_to_yards(double meters)
{
return 1.093613 * meters;
}

void display_mi_yd_in(double meteres)
{

}


I'm not entirely sure how to do the calculations in the void function which I want to write out the number of miles, yards, and inches. Since the numbers are doubles and can't use the % to get remainder I'm not sure how to proceed.
Last edited on
Hello, I see you have a function for converting meters to yards. So if you want to be able to print out the number of miles, yards, and inches you need to also calculate the number of miles and inches right. So maybe some functions like these:
1
2
double meters_to_miles(double meters);
double meters_to_inches();
and then pass those variables to void display_mi_yd_in(double meteres)
Last edited on
I'm not entirely following, I'm using a book thats meant for college that I got for cheap off a friend who took a class a long time ago. The problem reads Given a distance in meters, output the equivalent distance in miles, yards, and inches, accurate to the nearest inch. (There are 1.093613 yards per meter.) You may want to use the functions whose declarations are

double meters_to_yards (double meters);

void display_mi_yd_in (double meters)

I've had a hard time understanding this chapter without an actual teacher and the book is pretty old, in the preface it talks about floppy discs becoming a thing to give an idea of how old it is, and it seems as though coding has changed a lot since then.
The functions that the book recommends that you use seem to be missing a few unless they want you to do your conversions in void display_mi_yd_in (double meters). Try using three different functions to convert the meters to inches, yards and miles and then passing them to your display function. e.g.
1
2
3
double meters_to_miles(double meters);
double meters_to_inches(double meters); 
double meters_to_yards(double meters)
for your functions. Make each function do what it should so for example double meters_to_inches(double meters); would return the conversion for meters to inches. Then after you have gotten the conversions you can pass them to void display_mi_yd_in(double meters, double inches,double miles,double yards). For example a call to void display_mi_yd_in would look something like this: void display_mi_yd_in(m,i,d,m,y) // m would be a variable for meters, i for inches etc. . Hope this can help you
Last edited on
so something like this.
1
2
3
double meter_to_inches (double meters);
double meter_to_yards (double meters);
double meter_to_miles(double meters);


and then call down to the individual functions and return them. and then send them to
 
void display_mi_yd_in(double meters, double miles, double yards, double inches)

by using display_mi_yd_in(m,i,d,m,y) for example. That seems to compile fine when I do it, my next question is how to then break it down to say 10,000 meters is 6 miles 376 yards, 5 inches? I've only learned how to do remainder on integers for example

 
y=105%25 
where y equals 5. I can't use the % division on doubles which is where I run into issues. I've tried truncating the doubles into integers but that makes it return 6 miles 376 yards 10 inches which is incorrect.
Last edited on
The fmod(x,y) function is what is used instead of % for doubles. Use it like so y=fmod(105,25)
Ah that helps a lot, i'm still returning the value of 10 inches instead of 5 inches in the aforementioned 10000 meter conversion, but i'm assuming its a logic mistake for such a small error margin.

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
double meter_to_inches (double meters);
double meter_to_yards (double meters);
double meter_to_miles(double meters);
void display_mi_yd_in (double meters, double inches, double miles, double yards);

//dispaly meteres as miles, yards, and inches, rounded to nearest inch

int main()
{
	int iterations, m;
	double mi, yd, in;

	
	cout << "Enter the number of calculations to be performed. ";
	cin >> iterations;
	for (int i=1; i <= iterations; i = i + 1)
		{
			cout << "Enter the number of meters. ";
			cin >> m;
			mi = meter_to_miles(m);
			yd = meter_to_yards(m);
			in = meter_to_inches(m);
			display_mi_yd_in(m, mi, yd, in);			
		}


	return 0;
}

double meter_to_miles(double meters)
{
	return meters / 1609.344;
}

double meter_to_inches(double meters)
{
	return meters * 39.3701;
}

double meter_to_yards(double meters)
{
	return meters * 1.093613;
}


void display_mi_yd_in(double meters, double miles, double yards, double inches)
{
	double m, y, i;
	m = yards / 1760;
	y = fmod(yards, 1760);
	i = y / 36;
	cout << meters << " meters is " << m << " miles " << y << " yards and " << i << " inches" << endl;	
}


is the full program segment this concerns. the output comes out as 6.21371 miles 376.13 yards and 10.4481 inches. I'm not sure where my error is.
Last edited on
What if you change the 36 in this i = y / 36; to a 72.
ahh yes that works, however, rather than just taking it as is, I would like to know the why. I figured since 36 inches in a yard, that y/36 would be the right calculation. Thanks so much for your help.
Just to clarify are you trying to get your program to represent the meters in inches yards and miles. For example 100 meters = 0 miles, 109 yards and 14 inches(when all these are added up they are equal to 100 meters) or are you trying to have your program print the meters converted to each. e.g. 100 meters = 0.0621371 miles, 109.361 yards and 3937.01 inches?
trying to get it to do the first, as in 100 meters = 0 miles, 109 yards, and 14 inches.
Ignore my post above. Instead of that use i = fmod(inches, 36);. We use this because we want the remainder after dividing the amount of inches by 36(or a yard). This way we only get the number of inches left after we get rid of all the full yards which will be represented by double y.
Last edited on
Ok, that gets the answer even closer, it still seems slightly imperfect as I tried 5000 and it came out as 3 miles 188 yards and 2 inches, which is correct, however if i set precision to (2) the inches is 2.500 which means if I rounded it, it would go to 3 and the right answer is 2 when rounded as the precise answer is 3 miles 188 yd, 2.39 inches. so seems as though still a tad bit off and I want to add the floor(x+.05) rounding in without it spitting back the wrong answer. But you have been a big help, as I am much closer than ever.
Glad to Help, hope you can get it working :D.
Last edited on
Topic archived. No new replies allowed.