Question: (C++) need to get option D to make,sort and output a copy of the array. When i selectoption D the copy should output sorted data.

(C++) need to get option D to make,sort and output a copy of the array.

When i selectoption D the copy should output sorted data. Then when I choose option A I get the inital data straight from the file.

Data:

WHVC 34000.00 5 AAAA 10500.00 8 BURB 23500.00 2 CCCC 15000.00 4 DATA 8000.00 3 EEEE 36000.00 5 FADE 8500.00 4 GATE 25000.00 1 HILO 3000.00 1 JURY 100000.00 5 KNEL 80000 4 LIST 41000.00 3 MEMM 5000.00 2 PQRS 18000.00 2 RELM 27500.00 4 SOLD 22100.00 2

CODE:

#include

#include

#include

#include

using namespace std;

/**

* Structure to store the value of each house

*/

struct house

{

string id;

double income;

int members;

}family[100]; // creating families as object of house

void displayMenu();

void readFile(int &size);

void printFamilies(int size);

double getAverageIncome(int size);

void printFamilyMoreThanAverageIncome(int size, double average);

double getHouseHoldsLessThanPovertyLine(int size);

void printDataSortedByIncome(int size);

void printMedianIncome(int size);

int main()

{

int size = 0;

readFile(size);

// prompt and load file on startup

double avg = getAverageIncome(size);

// store calculated value in variable for re-use and faster response time.

double percPovertyLine = getHouseHoldsLessThanPovertyLine(size);

string choice;

do {

displayMenu();

cin >> choice;

//erase leading spaces(prefixed) from the entered string

choice.erase(0, choice.find_first_not_of(' '));

//erase trailing space(suffixed) from the entered string

choice.erase(choice.find_last_not_of(' ') + 1);

if ((choice.compare("A")) == 0)

printFamilies(size);

else if ((choice.compare("B")) == 0) {

cout << " Average income of all the families is: $" << avg << endl << endl;

cout << "Families more than the average income is below." << endl;

printFamilyMoreThanAverageIncome(size, avg);

}

else if ((choice.compare("C")) == 0)

cout << "Percentage of families below poverty line is: " << fixed << setprecision(2) << percPovertyLine << "%" << endl;

else if ((choice.compare("D")) == 0) {

cout << " Printing the families sorted by income" << endl;

printDataSortedByIncome(size);

}

else if ((choice.compare("E")) == 0)

printMedianIncome(size);

else if ((choice.compare("EXIT")) == 0)

break;

else

cout << "Invalid input!!";

} while ((choice.compare("EXIT")) != 0);

return 0;

}

void displayMenu() {

// cout << "Menu" << endl; //Temp menu

cout << "Please make a selection from the following menu options:" << endl

<< "(A) Display all input data formatted with columns and column headers." << endl

<< "(B) Display households with income greater than average household income." << endl

<< "(C) Display percentage of households below poverty level." << endl

<< "(D) Display all input data sorted by household income." << endl

<< "(E) Display the median household income." << endl

<< "Type a letter corresponding to your selection or type 'Exit' to quit, then press enter." << endl;

}

/**

* Function to read the file and populate the Structure array

*/

void readFile(int &size)

{

string filename;

ifstream infile;

cout << "Enter the filename you want to process: ";

cin >> filename;

infile.open(filename.c_str());

cout << "Searching File ... " << endl;

if (!infile) // if file was not found then print the error message and exit, ensure that file is in the workspace

{

cout << "The input file '" << filename << "' was not found" << endl;

exit(0);

}

else {

cout << "File found!" << endl;

}

int i = 0;

cout << " Reading file ... " << endl;

while (!infile.eof()) // read until end of file

{

infile >> family[i].id;

infile >> family[i].income;

infile >> family[i].members;

i++;

}

size = i;

infile.close();

cout << "File read successfully" << endl << endl;

}

/**

* Function to print the structure

*/

void printFamilies(int size)

{

cout << left << setw(15) << "Family ID" << setw(10) << "Income" << setw(10) << "Members" << endl;

for (int i = 0; i < size; i++)

{

cout << setw(15) << family[i].id << setw(10) << family[i].income << setw(10) << family[i].members << endl;

}

}

/**

* Family to calculate the average income

*/

double getAverageIncome(int size)

{

double sum = 0;

for (int i = 0; i < size; i++)

{

sum += family[i].income;

}

return (sum / size);

}

/**

* Function to print the households whose income is more than the average income

*/

void printFamilyMoreThanAverageIncome(int size, double average)

{

cout << endl;

cout << left << setw(15) << "Family ID" << setw(10) << "Income" << setw(10) << "Members" << endl;

for (int i = 0; i < size; i++)

{

if (family[i].income > average)

{

cout << left << setw(15) << family[i].id << setw(10) << family[i].income << setw(25) << family[i].members << endl;

}

}

}

/**

* Function to calculate and return the pecentage of households whose income is less than poverty line

*/

double getHouseHoldsLessThanPovertyLine(int size)

{

int count = 0;

for (int i = 0; i < size; i++)

{

double P = 8000 + (500 * (family[i].members - 2));

if (family[i].income < P)

{

count++;

}

}

return (count * 100) / (double)size;

}

/**

* Function to print the families sorted by the income

*/

void printDataSortedByIncome(int size)

{

int i, j;

for (i = 0; i < size; i++) {

for (j = 1; j < size - i; j++) {

if (family[j - 1].income > family[j].income) { // swap elements j and j+1

double temp = family[j - 1].income;

family[j - 1].income = family[j].income;

family[j].income = temp;

string ID = family[j - 1].id;

family[j - 1].id = family[j].id;

family[j].id = ID;

int member = family[j - 1].members;

family[j - 1].members = family[j].members;

family[j].members = member;

}

}

}

printFamilies(size);

}

/**

* Function to print the median income

*/

void printMedianIncome(int size)

{

double medianIncome;

if (size % 2 != 0) // if number of families are odd

{

int index = (size / 2);

medianIncome = family[index].income;

}

else // if number of families are even

{

int index1 = size / 2;

int index2 = index1 + 1;

medianIncome = (family[index1 - 1].income + family[index2 - 1].income) / 2;

}

cout << "Median income is: $" << medianIncome << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!