Units of Measure conversion

256 Views Asked by At

I was wondering if i could get some help trying to create a simple math formula. I recently was given an interview to work as a tier1 programmer and was asked to make a program. I made the whole thing perfectly fine but what made me fail the interview was that i need to create a math formula to convert different units of measure to other units of measure. This is easy, but what made it hard was that i needed to convert these different units of measure based off of the lengths given to me in feet. This is really bugging me because the guy said that i couldve just made one equation that couldve been used for all of the conversions to the different units of measurement. So, the current units of measurement that were given contained their orginal units of measurement and their measurement in terms of feet. The formula has to utilize the different lengths in terms of feet to do these conversions. Other wise, i wouldve got the job by just dividing and multiplying everything to get my answer. The different units of measure are as follows:

inch,.083333
fathom,6
foot,1
furlong,660
kilometer,3281.5
meter,3.2815
mile,5280
rod,16.5
yard,3

The numbers after the comma are the lengths in terms of feet for that unit of measure. I have been staring at this for hours, but i cannot figure out an equation to use for all the lengths. For those that give me help, i really do appreciate it, i am beginning to feel defeated with this. Thanks again.

3

There are 3 best solutions below

0
On BEST ANSWER

"input" means the quantity which is given. "infeet" is an auxiliary variable. The "output" is the requested quantity.

$$ \text{infeet}= \begin{cases} \frac{\text{input}}{1}&, \text{ if the input is given in feet}\\ \frac{\text{input}}{0.083333}&, \text{ if the input is given in inches}\\ \frac{\text{input}}{6}&, \text{ if the input is given in fathoms}\\ \frac{\text{input}}{660}&, \text{ if the input is given in furlongs}\\ \frac{\text{input}}{3281.5}&, \text{ if the input is given in kilometers}\\ \frac{\text{input}}{3.2815}&, \text{ if the input is given in meters}\\ \frac{\text{input}}{5280}&, \text{ if the input is given in miles}\\ \frac{\text{input}}{16,5}&, \text{ if the input is given in yards}\\ \frac{\text{input}}{3}&, \text{ if the input is given in yards}\\ \end{cases} $$

$$ \text{output}= \begin{cases} \text{infeet} \times {1}&, \text{ if the output is requested in feet}\\ \text{infeet}\times {0.083333}&, \text{ if the output is requested in inches}\\ \text{infeet}\times {6}&, \text{ if the output is requested in fathoms}\\ \text{infeet}\times{660}&, \text{ if the output is requested in furlongs}\\ \text{infeet}\times{3281.5}&, \text{ if the output is requested in kilometers}\\ \text{infeet}\times{3.2815}&, \text{ if the output is requested in meters}\\ \text{infeet}\times{5280}&, \text{ if the output is requested in miles}\\ \text{infeet}\times{16,5}&, \text{ if the input is requested in yards}\\ \text{infeet}\times{3}&, \text{ if the output is requested in yards}\\ \end{cases} $$

0
On

So for example, take the line

meter,3.2815

Thus we could program the conversion factor

feetpermeter = 3.2815

Then if we are given valueinmeters we convert to valueinfeet using

valueinfeet = valueinmeters * feetpermeter

or the other way around

valueinmeters = valueinfeet / feetpermeter

And when feet are not involved, use two conversions:

valueinrods = valueininches * feetperinch / feetperrod

0
On

Did you not generalize your code appropriately? Perhaps that is why you were not asked to return?

For instance, each of the configuration items are name-length pairs. I would expect to see something along the lines of

// given: input value in inVal, input unit in inUnit, output unit in outUnit
// config: the associative list units contains name-length pairs
// produce: output value in outVal
// end result: inVal inUnits is equal to outVal outUnits
outVal = inVal*units[inUnit].length/units[outUnit].length;

Then, if we had inVal = 5, inUnit = "mile", outUnit = "fathom", then outval would take on the value $5 \times 5280 \div 6 = 4400$.

This would be more readable and easier to maintain than code that used the values in a series of if-then statements. I believe the Unix units program does things in essentially this fashion (somewhat more complicated since it permits compound units such as meters per second and furlongs per fortnight):

You have: 299792458 m/s
You want: furlongs/fortnight
    * 1.8026175e+12
    / 5.5474886e-13