1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct householdData {
string ID_Code;
double householdIncome;
int numFamilyMembers;
};
void inputData(householdData[], ifstream &, int &);
void printData(householdData[], int &i);
void householdsAboveAvg(householdData[], int &i, double &, double &);
void determinePovertyLevel(householdData[], int &i);
void printSortedList(householdData list[], int &i);
void determineMedianIncome(householdData list[], int &i, double &median);
void sortList(householdData[], int &i);
int main()
{
double total, average, median;
householdData list[80];
int i;
ifstream inFile;
inFile.open("File.txt");
if (!inFile) {
cout << "\nError: Could not open file.\n";
}
inputData(list, inFile, i);
printData(list, i);
householdsAboveAvg(list, i, total, average);
determinePovertyLevel(list, i);
sortList(list, i);
printSortedList(list, i);
determineMedianIncome(list, i, median);
return 0;
}
void determineMedianIncome(householdData list[], int &i, double &median)
{
median = 0;
sortList(list, i);
if (i % 2 == 0) {
median = (list[(i / 2) - 1].householdIncome + list[i / 2].householdIncome) / 2;
}
else
median = list[i / 2].householdIncome;
cout << "nMedian is " << median << endl;
}
void printSortedList(householdData list[], int &i)
{
cout << "\nHousehold Incomes Sorted: \n";
for (int k = 0; k < i; k++) {
cout << list[k].ID_Code << " " << list[k].householdIncome << " " << list[k].numFamilyMembers << endl;
}}
void sortList(householdData list[], int &i)
{
int swapNumbers = -1;
for (int j = i; j > 0; j--) {
for (int k = 0; k < i - 2; k++) {
if (list[k].householdIncome > list[k + 1].householdIncome) {
swapNumbers = list[k + 1].householdIncome;
list[k + 1].householdIncome = list[k].householdIncome;
list[k].householdIncome = swapNumbers;
}
}
}
}
void determinePovertyLevel(householdData list[], int &i)
{
cout << "\nHouseholds below poverty level: \n";
for (int k = 0; k < i; k++) {
if (list[k].householdIncome <= (8000 + 500 * (list[k].numFamilyMembers - 2))) {
cout << setw(7) << list[k].ID_Code << setw(17) << list[k].householdIncome << setw(15) << list[k].numFamilyMembers << endl;
}
}
}
void householdsAboveAvg(householdData list[], int &i, double &total, double &average)
{
total = 0;
average = 0;
for (int k = 0; k < i; k++) {
total += list[k].householdIncome;
}
average = total / i;
cout << "\nThe average household income is: " << average << endl;
cout << "\nHouseholds above the average income: \n";
for (int k = 0; k < i; k++) {
if (list[k].householdIncome > average) {
cout << list[k].ID_Code << " " << list[k].householdIncome << endl;
}
}
}
void inputData(householdData list[], ifstream & inFile, int &i)
{
i = 0;
while (!inFile.eof()) {
inFile >> list[i].ID_Code >> list[i].householdIncome >> list[i].numFamilyMembers;
i++;
}
}
void printData(householdData list[], int &i)
{
cout << "\nID Code" << " Household Income " << "Family Members\n";
for (int k = 0; k < i; k++) {
cout << setw(7) << list[k].ID_Code << setw(17) << list[k].householdIncome << setw(15) << list[k].numFamilyMembers << endl;
}}
|