I need help finish this c++ program that computes and outputs the average househ
ID: 3560402 • Letter: I
Question
I need help finish this c++ program that computes and outputs the average household income for
each city to an output file. Down here is the incomplete code:
#include <iostream>
#include <iomanip>
using namespace std;
void initialize( // fill in the parameters );
void totalIncome( // fill in the parameters );
void averageIncome( // fill in the parameters );
void displayResults( // fill in the parameters );
double larger ( // fill in the parameters );
int main()
{
int countLV, countHD;
double totalLV, totalHD;
double averageLV, averageHD;
double topIncome;
cout << fixed << showpoint;
cout << setprecision(2);
initialize( // fill in the parameters );
totalIncome( // fill in the parameters );
averageIncome( // fill in the parameters );
displayResults( // fill in the parameters ) ;
return 0;
}
void initialize( // fill in the parameters )
{
// This function initializes variables.
//
// Supply the code.
}
void totalIncome ( // fill in the parameters )
{
//This function reads data from a file and finds the sum of household income for each city.
// It also finds the highest household income.
// This function calls the "larger" function.
//
// Supply the code.
}
double larger ( // fill in the parameters )
{
// This function returns larger number from given 2 numbers.
//
// Supply the code.
}
void averageIncome( // fill in the parameters )
{
// This function finds the average income for each city.
//
// Supply the code.
}
void displayResults(// fill in the parameters )
{
// This function outputs the relevant results.
//
// Supply the code.
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
//To initialize all variables to be zero
void initialize(int *,int *,double *,double *,double *,double *,double *);
//To count number of households and to get the total income of each city
void totalIncome(int *,int *,double *,double *,double *);
//To get the average income of each city given the total income and number of households
void averageIncome(int,int,double,double,double *,double * );
//To display the results in required format
void displayResults(int,int,double,double,double,double,double);
//To get the larger of two given numbers, used to find highest income
double larger (double,double);
int main()
{
int countLV, countHD;
double totalLV, totalHD;
double averageLV, averageHD;
double topIncome;
cout << fixed << showpoint;
cout << setprecision(2);
initialize( &countLV,&countHD,&totalHD,&totalLV,&averageHD,&averageLV,&topIncome);
totalIncome(&countLV,&countHD,&totalHD,&totalLV,&topIncome);
averageIncome(countLV,countHD,totalHD,totalLV,&averageHD,&averageLV );
displayResults( countLV,countHD,totalHD,totalLV,averageHD,averageLV,topIncome );
return 0;
}
void initialize( int *countLV,int *countHD,double *totalHD,double *totalLV,double *averageHD,double *averageLV,double *topIncome)
{
*countLV = 0;
*countHD = 0;
*totalLV = 0.0;
*totalHD = 0.0;
*averageLV = 0.0;
*averageHD = 0.0;
*topIncome = 0.0;
}
void totalIncome (int *countLV,int *countHD,double *totalHD,double *totalLV,double *topIncome)
{
string line,city,income;
cout << "Processing Household Income" << endl;
while(getline(cin,line)){
istringstream iss(line);
iss >> city;
iss >> income;
if(city == "HD"){
*countHD += 1;
double inc = stod(income);
*totalHD += inc;
*topIncome = larger(*topIncome,inc);
cout << line << endl;
}
else if(city == "LV"){
*countLV += 1;
double inc = stod(income);
*totalLV += inc;
*topIncome = larger(*topIncome,inc);
cout << line << endl;
}
else{
cout << "Bad Data" << endl;
}
}
}
double larger ( double a, double b)
{
if (a >= b)
return a;
else
return b;
}
void averageIncome( int countLV,int countHD,double totalHD,double totalLV,double *averageHD,double *averageLV)
{
if(countLV != 0)
*averageLV = (totalLV)/(countLV);
if(countHD != 0)
*averageHD = (totalHD)/(countHD);
}
void displayResults(int countLV,int countHD,double totalHD,double totalLV,double averageHD,double averageLV,double topIncome)
{
cout << endl;
cout << "***********************************" << endl;
cout << " Trung Nguyen Assignment 8 " << endl;
cout << "***********************************" << endl;
cout << endl;
cout << " Henderson " << endl;
cout << "Number of households: " << countHD << endl;
cout << "Total Household Income: $" << totalHD << endl;
cout << "Average Household Income: $" << averageHD << endl;
cout << "***********************************" << endl;
cout << endl;
cout << " Las Vegas " << endl;
cout << "Number of households: " << countLV << endl;
cout << "Total Household Income: $" << totalLV << endl;
cout << "Average Household Income: $" << averageLV << endl;
cout << "***********************************" << endl;
cout << endl;
cout << "Top Household Income: $" << topIncome << endl;
cout << "***********************************" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.