hello, I am writing a unit conversion program (feet,inches, meters miles)
the program is to enter what the user wants to convert from and what they want to convert to, and then how many unit there are starting out.
so the user could say they want to convert 2 miles into inches.
I am trying to do this with a 2d array making a conversion table but my math is just not coming out right.
and I am kind of lost, here's the idea
convert from?
1)inches
2)feet
3)meters
4)miles
convert to
1)inches
2)feet
3)meters
4)miles
number of units? (this is the starting number of units)
so if the user enters 1 and 3 and then 12, means 12 inches to how many meters.
my plan is to have the input be the array indexes(-1, cause array's start at zero) so 1 and 3 would be array index [0][2] in [0][2] i have the conversion.
so i multiply that number by units and boom.
but its not working im getting 144feet for 12inches to feet, I was getting the correct number for some of the such as the number of feet in a mile.
this leads me to believe that the table is not set up correctly.
any help would be great just need help with the theory behind this conversion table thanks.
Consider doing 2 conversions - sounds mad, but here me out.
Say you have 10 distance units. To convert from any one of them to any other one you need a 10 x 10 matrix = 100 items .
Pick a common unit to use. If you like imperial choose feet, if you are a metric guy choose metres. say we go with feet.
Now the idea is to convert the input units to feet and then convert this to the target units. Sounds mad still? Bear with me. The advantage is that now you only need a 10 x 2 array = 20 items, or if you wish two 10 x 1 arrays.
Use one array to store the conversion values for each unit to feet. Use the other one to do conversion from feet to the target unit. Or put it all in the 10 x 2 array.
The down side is there are 2 function calls.
I just realised I am saying the same thing as ne555:
Maybe it would be better to work in meters as an intermediate step.