Q1. Give the function header for each of the following functions: 1 2 3 4 Functi
ID: 3915655 • Letter: Q
Question
Q1. Give the function header for each of the following functions: 1 2 3 4 Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result. Function greatest that takes three integers, x, y and z, and returns an integer Function instructions that does not receive any arguments and does not return a value. Function IntToDouble that takes an integer argument, number, and returns a double-precision, floating- point result.Explanation / Answer
// The array access is 0 indexed
// But we report the user with 1 indexed
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
/*
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
*/
/**
* 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;
}
/**
* Compare function takes two elements and compares them
* Here the elements will be structs of families
*/
int compareFunc(const void* e1, const void* e2) {
struct house *family1 = (struct house*) e1;
struct house *family2 = (struct house*) e2;
return family1->income-family2->income;
}
/**
* Function to print the families sorted by the income
*/
void printDataSortedByIncome(int size) {
int i, j;
// qsort is a predefined method which is used to sort any predefined and
// userdefined type arrays.
// it takes 4 arguments
// 1 : name of the array to be sorted
// 2 : size of the array to be sorted
// 3 : size of the each element in the array
// 4 : a function name in which we put the logic based on which
// we compare the array elements
qsort(family, size, sizeof(struct house), compareFunc);
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.