Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment #2: Unit Conversion Tables This programming project will

ID: 3594027 • Letter: P

Question

Programming Assignment #2: Unit Conversion Tables

This programming project will allow the user to create a table of unit conversions, where the user specifies:

the dimension (example: length)

the starting unit (example: ft)

ending unit (example: m)

the starting value (example: 0)

the ending value (example: 10)

the increment (1)

the number of digits after the decimal point for the starting unit (example: 1)

the number of digits after the decimal point for the ending unit (example: 3)

For the example above, the output might be similar to the table shown below:

Length (ft)

Length (m)

0.0

0.000

1.0

0.305

2.0

0.610

3.0

0.914

4.0

1.219

5.0

1.524

6.0

1.829

7.0

2.134

8.0

2.438

9.0

2.743

10.0

3.048

Program Requirements:

The program should provide an initial description.

The user should be prompted to enter the dimension (length, mass, or time).

The user should be given a choice of initial units and final units for each dimension as follows:

Length: m, ft, cm or in

Mass: kg, g or slug

Time: h, min or s

         Note that the initial unit cannot be the same as the final unit, but there are still many possible conversions. For example, with length there are 12 possible conversions

m to ft

ft to m

cm to m

in to m

m to cm

ft to cm

cm to ft

in to ft

m to in

ft to in

cm to in

in to cm

         Similarly, there are multiple conversion possibilities for mass and time.

The units should be read as strings and any combination of uppercase and lowercase letters can be used. The user should be able to use the full unit name (plural or singular) or the symbol (for example, meters, meter or m). Use a function to convert the input unit to all lowercase letters. Only the unit symbol should be used in the table.

There are many sources for conversion factors. Use the conversion factors in the unit conversion program Digital Dutch (https://www.digitaldutch.com/unitconverter/length.htm).

         An example is shown below. If a value of 1 ft is entered and converted to m, we see that the conversion factor is 0.3048.

4.       The table should include a heading with the dimension name and the units. The number of digits displayed must match the user’s specifications. The table should be right justified and all decimal points should be aligned. See the sample table on the previous page.

5.       The program should calculate the number of lines in the table based on the user inputs specifying starting value, ending value and increment. The number of lines in the table must be at least 3, but not more than 25. If the ending value is not a multiple of the increment, the table should end with the last possible value before the ending value specified. For example, if the user specifies the following value:

          Starting value: 10

          Ending value: 100

          Increment: 20

          The program should determine that 5 lines are needed and that the last value in the table should be 90 (not 110).

Use loops allow the user to correct any bad inputs (and provide a suitable error message). Bad inputs should include:

Choice of dimension (length, mass or time)

Choice of units (with correct spelling) for each dimension

Starting and ending unit cannot be the same

Number of digits > 0

Starting value, increment and ending value must all be positive.

Number of lines in the table must be from 3 to 25. If this condition fails, the user should re-enter the starting value, increment and ending value.

Give the user the option of re-running the program.

Efficiency: The program must be efficient or else it could become exceeding long and significant grade penalties will apply. For example, a poorly written program might repeat the code for generating a table dozens of times for the many possible conversions. An efficient program would only have one set of instructions to generate a table and the table would use the assigned values for dimension, units, digits, starting value, etc.

Give the user the option of re-running the program (use a do while loop).

The program should use at least 3 functions, including:

Function to convert a string to all lower case.

Function that returns the appropriate conversion factor based on the starting unit and ending unit.

Function of your choice (it must be useful and well-designed).

                                                                            Page 3

Test Cases:

Run the program for the following test cases:

Dimension

Starting Unit

Ending Unit

Starting Value

Ending Value

Increment

# digits (starting unit)

# digits (ending unit)

length

ft

m

0

10

1

1

3

length

METER

feet

100

300

30

0

1

time

H

S

0

4

0.2

1

0

time

second

MIN

5000

10000

400

0

0

mass

kilogram

Slug

0

1

0.05

2

4

mass

KG

Gram

1

5

0.25

2

0

Note: The first test case is the same as the example shown on page 1

* Using C++ programming

Length (ft)

Length (m)

0.0

0.000

1.0

0.305

2.0

0.610

3.0

0.914

4.0

1.219

5.0

1.524

6.0

1.829

7.0

2.134

8.0

2.438

9.0

2.743

10.0

3.048

Explanation / Answer

Hi,

Code is self explanatory. Easy Cout and Cin statements with function and loops.

Explanation:

1. Get the Dimension from the user. If user misspells it, application will ask the user again instead exiting the application

2. Similarly application gets the starting and ending unit.

3. Application gets the step, starting value, ending value, precision from the user.

Based on the input, application decides the multiplier, then prints the table.

Code:

#include <iostream>

#include<string>

#include<bits/stdc++.h>

using namespace std;

string convert(string unit){

transform(unit.begin(), unit.end(), unit.begin(), ::tolower);;

if(unit == "m" || unit == "meter" || unit == "meters"){

return "meter";

}else if(unit == "ft" || unit == "feet" || unit =="feets"){

return "feet";

}else if(unit == "centimeter" | unit == "cm" || unit == "centimeters"){

return "centimeter";

}else if(unit == "inch" || unit == "in" || unit == "inches"){

return "inch";

}else if(unit == "hour" || unit == "hours" || unit =="h"){

return "hour";

}else if(unit == "m" || unit =="minute" || unit == "minutes"){

return "minute";

}else if(unit == "s" || unit == "second" || unit =="seconds"){

return "second";

}else if(unit == "kg" || unit =="kilogram" || unit =="kilograms"){

return "kilogram";

}else if(unit == "gram" || unit =="g" || unit =="grams"){

return "gram";

}else if(unit =="slug" || unit =="slugs"){

return "slug";

}else if(unit =="length" || unit =="lengths"){

return "length";

}else if(unit =="mass"){

return "mass";

}else if(unit =="time" || unit =="times"){

return "time";

}

return "invalid";

}

string getAbr(string unit){

if(unit == "meter"){

return "m";

}else if(unit == "centimeter"){

return "cm";

}else if(unit == "inch"){

return "in";

}else if(unit == "feet"){

return "ft";

}else if(unit == "gram"){

return "g";

}else if(unit == "kilogram"){

return "kg";

}else if(unit == "slug"){

return "slug";

}else if(unit == "hour"){

return "h";

}else if(unit == "minute"){

return "m";

}else if(unit == "second"){

return "s";

}

return "unit";

}

void displayTable(string sUnit, string eUnit, float sValue, float eValue, float step, int precision){

string sUnitAbr=getAbr(sUnit), eUnitAbr=getAbr(eUnit);

float multiplier = 0.0;

if(sUnit == "meter"){

if(eUnit=="feet"){

multiplier = 3.28;

}else if(eUnit=="inch"){

multiplier = 39.37;

}else if(eUnit =="centimeter"){

multiplier = 100;

}

}

  

else if(sUnit == "feet"){

if(eUnit=="meter"){

multiplier = 0.3048 ;

}else if(eUnit=="inch"){

multiplier = 12;

}else if(eUnit =="centimeter"){

multiplier = 30.48;

}

}

  

else if(sUnit == "inch"){

if(eUnit=="feet"){

multiplier = 0.083;

}else if(eUnit=="meter"){

multiplier = 0.0254;

}else if(eUnit =="centimeter"){

multiplier = 2.54;

}

}

  

else if(sUnit == "centimeter"){

if(eUnit=="feet"){

multiplier = 0.0328;

}else if(eUnit=="inch"){

multiplier = 0.3937;

}else if(eUnit =="meter"){

multiplier = 0.01;

}

}

  

else if(sUnit == "gram"){

if(eUnit=="kilogram"){

multiplier = 0.001;

}else if(eUnit=="slug"){

multiplier = 0.0000685;

}

}

  

else if(sUnit == "kilogram"){

if(eUnit=="gram"){

multiplier = 1000;

}else if(eUnit=="slug"){

multiplier = 0.0685;

}

}

  

else if(sUnit == "slug"){

if(eUnit=="kilogram"){

multiplier = 14.5939;

}else if(eUnit=="gram"){

multiplier = 14593.9;

}

}

  

else if(sUnit == "hour"){

if(eUnit=="minute"){

multiplier = 60;

}else if(eUnit=="second"){

multiplier = 3600;

}

}

  

else if(sUnit == "minute"){

if(eUnit=="hour"){

multiplier = 0.0166;

}else if(eUnit=="second"){

multiplier = 60;

}

}

  

else if(sUnit == "second"){

if(eUnit=="minute"){

multiplier = 0.0166;

}else if(eUnit=="hour"){

multiplier = 0.0002777;

}

}

cout<<sUnit+"("<<sUnitAbr<<") "+eUnit+"("<<eUnitAbr<<")";

cout.precision(precision);

for(;sValue <= eValue; sValue += step){

float temp = (sValue*multiplier);

cout<<" "<<sValue<<" "<<temp;

}

}

int main() {

cout<<"This application converts one input unit to another input unit for the given starting and ending unit and the step. ";

bool b = true;

string dimension="", sUnit="", eUnit="";

int dIndex = 0, precision;

float step, sValue, eValue;

do{

cout<<"Enter the Dimension 1. Length 2. Mass 3. Time ";

cin>>dimension;

cout<<"Selected Dimension is "+dimension+" ";

b=false;

dimension = convert(dimension);

while(dimension == "invalid"){

cout<<"Entered Dimesnion is wrong.Please check spelling and re-enter the dimension: [Length || Mass || TIme] ";

cin>>dimension;

dimension = convert(dimension);

}

if(dimension == "length"){

dIndex = 1;

}else if(dimension == "mass"){

dIndex = 2;

}else if(dimension == "time"){

dIndex = 3;

}

cout<<dIndex;

switch(dIndex){

case 1:

cout<<"Select the starting unit 1. Meter or m or meters 2. Feet or ft or feets 3. CentiMeter or cm or centimeters 4. Inch or in or inches ";

cin >> sUnit;

cout<<"Select the ending unit 1. Meter or m or meters 2. Feet or ft or feets 3. CentiMeter or cm or centimeters 4. Inch or in or inches ";

cin >> eUnit;

sUnit = convert(sUnit);

while(sUnit == "invalid"){

cout<<"Please select the correct starting Unit. ";

cin >> sUnit;

}

eUnit = convert(eUnit);

while(eUnit == "invalid"){

cout<<"Please select the correct ending Unit. ";

cin >> eUnit;

}

while(sUnit == eUnit){

cout<<"ENding unit should not be same as starting unit. Please Select the ending unit 1. Meter or m or meters 2. Feet or ft or feets 3. CentiMeter or cm or centimeters 4. Inch or in or inches ";

cin >> eUnit;

eUnit = convert(eUnit);

}

break;

case 2:

cout<<"Select the starting unit 1. Kilogram or kg or kilograms 2. Gram or g or grams 3.Slug or slugs ";

cin >> sUnit;

cout<<"Select the ending unit 1. Kilogram or kg or kilograms 2. Gram or g or grams 3.Slug or slugs ";

cin >> eUnit;

sUnit = convert(sUnit);

while(sUnit == "invalid"){

cout<<"Please select the correct starting Unit. ";

cin >> sUnit;

}

eUnit = convert(eUnit);

while(eUnit == "invalid"){

cout<<"Please select the correct ending Unit. ";

cin >> eUnit;

}

while(sUnit == eUnit){

cout<<"ENding unit should not be same as starting unit. Please Select the ending unit 1. Kilogram or kg or kilograms 2. Gram or g or grams 3.Slug or slugs ";

cin >> eUnit;

eUnit = convert(eUnit);

}

break;

case 3:

cout<<"Select the starting unit 1. hour or h or hours 2. min or minutes or m 3. seconds or s or second ";

cin >> sUnit;

cout<<"Select the ending unit 1. hour or h or hours 2. min or minutes or m 3. seconds or s or second ";

cin >> eUnit;

sUnit = convert(sUnit);

while(sUnit == "invalid"){

cout<<"Please select the correct starting Unit. ";

cin >> sUnit;

}

eUnit = convert(eUnit);

while(eUnit == "invalid"){

cout<<"Please select the correct ending Unit. ";

cin >> eUnit;

}

while(sUnit == eUnit){

cout<<"ENding unit should not be same as starting unit. Please Select the ending unit 1. hour or h or hours 2. min or minutes or m 3. seconds or s or second ";

cin >> eUnit;

eUnit = convert(eUnit);

}

break;

default:

cout<<"Entered DImension seems wrong."+dIndex;

}

b = false;

}while(b);

  

cout<<" Eneter the increment or step ";

cin>>step;

cout<<"Enter the starting value ";

cin>>sValue;

cout<<"Enter the ending Value ";

cin>>eValue;

cout<<"Enter the precision ";

cin>>precision;

displayTable(sUnit,eUnit,sValue,eValue,step,precision);

}