Write a program that reads in a weight in pounds and ounces and outputs the equi
ID: 3831501 • Letter: W
Question
Write a program that reads in a weight in pounds and ounces and outputs the equivalent weight in kilograms and grams. Use at least three func- tions: one for input, one or more for calculating, and one for output. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. There are 2.2046 pounds in a kilogram, 1000 grams in a kilogram, and 16 ounces in a pound.
Write a program like that of the previous exercise that converts from kilo- grams and grams into pounds and ounces. Use functions for the subtasks.
(You should do the previous two Practice Programs before doing this one.) Write a program that combines the functions of the previous two Practice Programs. The program asks the user if he or she wants to con- vert from pounds and ounces to kilograms and grams or from kilograms and grams to pounds and ounces. The program then performs the desired conversion. Have the user respond by typing the integer 1 for one type of conversion and 2 for the other. The program reads the user’s answer and then executes an if-else statement. Each branch of the if-else statement will be a function call. The two functions called in the if-else statement will have function definitions that are very similar to the programs for the previous two Practice Programs. Thus, they will be function definitions that call other functions in their function bodies. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program.
Explanation / Answer
1.
#include <iostream>
#include <iomanip>
using namespace std;
void input(double £s,double &ounces) //reference variables as parameters
{
cout<<" Enter pounds : ";
cin>>pounds;
cout<<" Enter ounces : ";
cin>>ounces;
}
void conversion(double £s, double &ounces, double &kilograms, double &grams)
{
pounds = pounds + ounces/16;
kilograms = pounds * 0.453592; // pounds to kilograms
grams = (kilograms - (int)kilograms)*1000;
kilograms = (int)kilograms;
}
void output(double &kilograms,double &grams)
{
cout<<" Kilograms : "<<kilograms;
cout<<" grams : "<<grams;
}
int main()
{
double pounds;
double ounces;
double kilograms;
double grams;
int Continue = 0;
do
{
input(pounds, ounces);
conversion(pounds, ounces, kilograms, grams);
output(kilograms, grams);
cout<<" Do you want to continue <1- yes, 0- No>";
cin>>Continue;
if(Continue == 0)
break;
}while(Continue!= 0);
return 0;
}
Output:
Enter pounds : 46
Enter ounces : 8
Kilograms : 21
grams : 92.028
Do you want to continue <1- yes, 0- No>0
2.
#include <iostream>
#include <iomanip>
using namespace std;
void input(double &kilograms,double &grams) //reference variables as parameters
{
cout<<" Enter kilograms : ";
cin>>kilograms;
cout<<" Enter grams : ";
cin>>grams;
}
void conversion(double £s, double &ounces, double &kilograms, double &grams)
{
kilograms = kilograms +grams/1000;
pounds = kilograms *2.2046; // kilograms to pounds
ounces = (pounds - (int)pounds)*16;
pounds = (int)pounds;
}
void output(double £s,double &ounces)
{
cout<<" Pounds : "<<pounds;
cout<<" Ounces : "<<ounces;
}
int main()
{
double pounds;
double ounces;
double kilograms;
double grams;
int Continue = 0;
do
{
input(kilograms,grams);
conversion(pounds, ounces, kilograms, grams);
output(pounds,ounces);
cout<<" Do you want to continue <1- yes, 0- No>";
cin>>Continue;
if(Continue == 0)
break;
}while(Continue!= 0);
return 0;
}
output:
Enter kilograms : 21
Enter grams : 92
Pounds : 46
Ounces : 7.99077
Do you want to continue <1- yes, 0- No>
3.
#include <iostream>
#include <iomanip>
using namespace std;
void inputPound(double £s,double &ounces) //reference variables as parameters
{
cout<<" Enter pounds : ";
cin>>pounds;
cout<<" Enter ounces : ";
cin>>ounces;
}
void conversionPoundToKgs(double £s, double &ounces, double &kilograms, double &grams)
{
pounds = pounds + ounces/16;
kilograms = pounds * 0.453592; // pounds to kilograms
grams = (kilograms - (int)kilograms)*1000;
kilograms = (int)kilograms;
}
void outputKgs(double &kilograms,double &grams)
{
cout<<" Kilograms : "<<kilograms;
cout<<" grams : "<<grams;
}
void output(double £s,double &ounces)
{
cout<<" Pounds : "<<pounds;
cout<<" Ounces : "<<ounces;
}
void inputKgs(double &kilograms,double &grams) //reference variables as parameters
{
cout<<" Enter kilograms : ";
cin>>kilograms;
cout<<" Enter grams : ";
cin>>grams;
}
void conversionkgToPound(double £s, double &ounces, double &kilograms, double &grams)
{
kilograms = kilograms +grams/1000;
pounds = kilograms *2.2046; // kilograms to pounds
ounces = (pounds - (int)pounds)*16;
pounds = (int)pounds;
}
void outputPound(double £s,double &ounces)
{
cout<<" Pounds : "<<pounds;
cout<<" Ounces : "<<ounces;
}
int main()
{
double pounds;
double ounces;
double kilograms;
double grams;
int Continue = 0;
int choice;
do
{
cout<<" 1.Convert Kilograms and grams to Pounds and Ounces";
cout<<" 2.Convert Pounds and Ounces to Kilograms an grams";
cout<<" Enter your choice <1 or 2>";
cin>>choice;
switch(choice)
{
case 1:
inputKgs(kilograms,grams);
conversionkgToPound(pounds, ounces, kilograms, grams);
outputPound(pounds,ounces);
break;
case 2:
inputPound(pounds,ounces);
conversionPoundToKgs(pounds, ounces, kilograms, grams);
outputKgs(kilograms,grams);
break;
default : cout<<" Invalid option";
break;
}
cout<<" Do you want to continue <1- yes, 0- No>";
cin>>Continue;
if(Continue == 0)
break;
}while(Continue!= 0);
return 0;
}
Output:
1.Convert Kilograms and grams to Pounds and Ounces
2.Convert Pounds and Ounces to Kilograms an grams
Enter your choice <1 or 2>1
Enter kilograms : 21
Enter grams : 92
Pounds : 46
Ounces : 7.99077
Do you want to continue <1- yes, 0- No>0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.