Modify the following C++ program so that it uses two value-returning functions;
ID: 3681484 • Letter: M
Question
Modify the following C++ program so that it uses two value-returning functions; one to determine the fat calories and the other to determine the fate percentage.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//declare variables
Int totalCals = 0;
Int fatGrams = 0;
Int fatCals = 0;
Double fatPerecent = 0.0;
//enter input items
cout << “Total calories: “;
cin >> totalCals;
cout<<”Grams of fat:”;
cin>>fatGrams;
//determine whether data is valid
If (totalCals >= 0 && fatGrams >= 0)
{
//calculate and display the output
fatCals = fatGrams*9;
fatPercent = static_cast<double>(fatCals)
/ static_cast<double>(totalCals)*100;
cout << “Fat calories:” << fatCals<<endl;
cout << fixed<<setprecision(0);
cout << “Fat percentage: “<<fatPercent
<<%<<endl;
}
else
cout<<”Input erroer”<<endl;
//end if
system (“pause”);
return 0;
} //end of main function
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//function prototype
int getFatCals(int);
double getFatPercent(int, int);
int main()
{
//declare variables
int totalCals = 0;
int fatGrams = 0;
int fatCals = 0;
double fatPercent = 0.0;
//get input items
cout << "Total Calories: ";
cin >> totalCals;
cout << "Grams of fat: ";
cin >> fatGrams;
if (totalCals >= 0 && fatGrams >= 0)
{
//call function to Fat calories and fat Percentage
fatCals = getFatCals(fatGrams);
fatPercent = getFatPercent(fatCals, totalCals);
//display
cout << fixed << setprecision(2) << endl;
cout << "Fat Calories: " << fatCals << endl;
cout << "Fat Percent: " << fatPercent << endl;
}
else
{
cout<<"Input erroer"<<endl;
}
//end if
system("pause");
return 0;
} //end of main function
//*****function definitions*****
int getFatCals(int fatGram)
{
//calculates and returns fat grams
int getFatCals = 0;
getFatCals = fatGram * 9;
return getFatCals;
} //end of getFatCals function
double getFatPercent(int fatCal, int totalCal)
{
//calculates and returns fat percent
double getFatPercent = 0.0;
getFatPercent = static_cast<double>(fatCal)/ static_cast<double>(totalCal) * 100;
return getFatPercent;
} //end of getFatPercent function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.