Lectorial 2: A Simple Unit Conversion Program In this lectorial we will be start
ID: 3700609 • Letter: L
Question
Lectorial 2: A Simple Unit Conversion Program In this lectorial we will be starting the unit conversion program project which is the major assessment task for the first half of this semester In this session just create a sequential script that converts a temperature (a hardcoded numeric value) from Fahrenheit to Celsius Test that your program outputs the correct values by using the following small set of test data to compare with your results: Sample test data Try to write another few scripts to create unit converters for the following types of units: To imperial To metric TT-32)x Lx2.54 2.54 mfeet Lx0.3048 0.3048 kmmiles a_ # D..? 1.609344 1.609344 Mass 71 "-# 283495 m28.3495 gramsounces NI kgpounds # 04536 km/hmph 1.609344 Volume V. litre >gallon 3.78544 Area hectrare acre A A x2.4711 2.4711Explanation / Answer
#include <iostream>
using namespace std;
//convert far to degree
double far2degree(double temp){
double degree;
degree=(temp- 32) * 5/9;
return degree;
}
//convert degree to far
double degree2far(double temp){
double far;
far = (temp * 9.0) / 5.0 + 32;
return far;
}
//convert cm to inch
double cm2inch(double cm){
double inch;
inch = (cm/2.54);
return inch;
}
// convert inch to cm
double inch2cm(double inch){
double cm;
cm=(cm*2.54);
return cm;
}
//convert meter to feet
double m2feet(double m){
double feet;
feet=m/0.3048;
return feet;
}
//convert feet to meter
double feet2m(double feet)
{
double m;
m= feet*0.3048;
return m;
}
//kilometer to miles
double km2m(double km){
double mile;
mile= km*1.609344;
return mile;
}
// mile into kilometer
double m2km(double mile)
{
double km;
km=mile/1.609344;
return km;
}
// gram to ounce convertor
double gram2ounce(double gram){
double ounce;
ounce=gram/28.3495;
return ounce;
}
// ounce to gram
double ounce2gram(double ounce)
{
double gram;
gram=ounce*28.3495;
return gram;
}
//convert kilgram to pound
double kg2pound(double kg)
{
double pound;
pound = kg/0.4536;
return pound;
}
// pound to kg convertor
double pound2kg(double pound)
{
double kg;
kg=pound*0.4536;
return kg;
}
//convert kilometer per hour to Mile per hour
double kmh2mph(double kmh){
double mph;
mph=kmh/1.609344;
return mph;
}
// convert Mile per hour to kilometer per hour
double mph2kmh(double mph){
double kmh;
kmh=mph*1.609344;
return kmh;
}
// liter to gallon convertor
double litre2gallon(double liter){
double gallon;
gallon = (liter/1.609344);
return gallon;
}
// gallon to liter convertor
double gallon2liter(double gallon)
{
double liter;
liter = (gallon*3.78544);
return liter;
}
//hectare to acre convertor
double hec2acre(double hec)
{
double acre;
acre = hec*2.4711;
return acre;
}
double acre2hec(double acre){
double hec;
hec=acre/2.4711;
return hec;
}
// function to print the result
void print (double result){
cout<<"Its conversion in Imperial System is: "<<result<<endl;
}
// For getting input from user of value to be converted
double input(){
double value;
cout<<"Enter the Value: ";
cin>>value;
return value;
}
//driver Code
int main(int argc, char const *argv[])
{
int choice, metric2eng, dchoice, flag=1;
double input1;
// double cm,m,far,degree,inch,feet,km,mile,gram,ounce,kg,pound,kmh,mph,liter,gallon;
cout<<"===========Welcome to Unit conversion Software.=========== ";
cout<<"Press 1 for converting Metric to Imperial System. ";
cout<<"Press 2 for converting Imperial to Metric system. ";
cout<<"Press 3 for Exit ";
cin>>choice;
if (choice == 1){
cout<<"===Enter you choice from following:=== ";
cout<<"1 Temparature: Celsius to Fahrenheit. ";
cout<<"2 Length and Distance Menu. ";
cout<<"3 Mass conversion Menu. ";
cout<<"4 Kilometer per hour(km/h) to Miles per hour(M/hr). ";
cout<<"5 Liter to Gallon. ";
cout<<"6 Hectrate to Acre. ";
cin>>metric2eng;
// Metric to Imperial conversion menu
switch (metric2eng)
{
case 1:
print(far2degree(input()));
break;
case 2:
cout<<"1 Centimeter to Inch. ";
cout<<"2 Meter to Feet. ";
cout<<"3 Kilometer to Miles ";
cin>>dchoice;
//distance conversion menu
switch (dchoice){
case 1:
print(cm2inch(input()));
break;
case 2:
print(m2feet(input()));
break;
case 3:
print(km2m(input()));
break;
default:
cout<<"You have not entered a valid choice. Retry Again ";
flag =0;
return 0;
}
break;
case 3:
cout<<"1 Grams to Ounce. ";
cout<<"2 Kilogram to Pounds. ";
cin>>dchoice;
//Weight conversion menu
switch (dchoice){
case 1:
print(gram2ounce(input()));
break;
case 2:
print(kg2pound(input()));
break;
default:
cout<<"You have not entered a valid choice. Retry Again ";
flag =0;
return 0;
}
break;
// speed conversion menu
case 4:
print(kmh2mph(input()));
break;
case 5:
print(litre2gallon(input()));
break;
case 6:
print(hec2acre(input()));
break;
default:
cout<<"You have not entered a valid choice. Retry Again ";
flag =0;
return 0;
}
}
// this menu will check for converting Imperial to Metric System
else if(choice == 2){
cout<<"===Enter you choice from following:=== ";
cout<<"1 Temparature: Fahrenheit to Celsius. ";
cout<<"2 Length and Distance Menu. ";
cout<<"3 Mass conversion Menu. ";
cout<<"4 Miles per hour(M/hr) to Kilometer per hour(km/h). ";
cout<<"5 Gallon to liter. ";
cout<<"6 Acre to Hectrate. ";
cin>>metric2eng;
switch (metric2eng)
{
case 1:
print(degree2far(input()));
break;
case 2:
//distance conversion menu
cout<<"1 Inch to Centimeter. ";
cout<<"2 Feet to Meter. ";
cout<<"3 Miles to Kilometer ";
cin>>dchoice;
switch (dchoice){
case 1:
print(inch2cm(input()));
break;
case 2:
print(feet2m(input()));
break;
case 3:
print(m2km(input()));
default:
cout<<"You have not entered a valid choice for converting Distance. Retry Again ";
flag =0;
return 0;
}
break;
case 3:
// Weight conversion menu
cout<<"1 Ounce to Grams. ";
cout<<"2 Pounds to Kilogram. ";
cin>>dchoice;
switch (dchoice){
case 1:
print(ounce2gram(input()));
break;
case 2:
print(pound2kg(input()));
break;
default:
cout<<"You have not entered a valid choice for converting mass. Retry Again ";
flag =0;
return 0;
}
break;
case 4:
print(mph2kmh(input()));
break;
case 5:
print(gallon2liter(input()));
break;
case 6:
print(acre2hec(input()));
break;
default:
cout<<"You have not entered a valid choice to convert in Meteric System. Retry Again ";
flag =0;
return 0;
}
}
// choice 3 will terminate the program
else if (choice == 3)
return 0;
else
cout<<"You have not Entered a valid choice for conversion. Try Again ";
return 0;
}
Output Of the Code:
dps@machine:~/Documents/Chegg$ ./simple_conversion
===========Welcome to Unit conversion Software.===========
Press 1 for converting Metric to Imperial System.
Press 2 for converting Imperial to Metric system.
Press 3 for Exit
1
===Enter you choice from following:===
1 Temparature: Celsius to Fahrenheit.
2 Length and Distance Menu.
3 Mass conversion Menu.
4 Kilometer per hour(km/h) to Miles per hour(M/hr).
5 Liter to Gallon.
6 Hectrate to Acre.
1
Enter the Value: 100
Its conversion in Imperial System is: 37.7778
dps@machine:~/Documents/Chegg$ ./simple_conversion
===========Welcome to Unit conversion Software.===========
Press 1 for converting Metric to Imperial System.
Press 2 for converting Imperial to Metric system.
Press 3 for Exit
2
===Enter you choice from following:===
1 Temparature: Fahrenheit to Celsius.
2 Length and Distance Menu.
3 Mass conversion Menu.
4 Miles per hour(M/hr) to Kilometer per hour(km/h).
5 Gallon to liter.
6 Acre to Hectrate.
3
1 Ounce to Grams.
2 Pounds to Kilogram.
2
Enter the Value: 100
Its conversion in Imperial System is: 45.36
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.