### Average and Percentage Issues

In this program, I have to display the information given(this part of my code works fine), find the average income and display the household ID Numbers that exceed the average, and display which household ID Numbers are below poverty level as well as the percentage of households that are in poverty.

My information displays perfectly. The average calculation seems to work fine. How would I be able to display which ID Numbers exceed the average?

I'm quite lost when it comes to the poverty level part.

Here is my code so far...
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include using namespace std; struct Household { int idNumber; int income; int members; }; char again; int main() { do { //Displaying Survey Information Household houses[13] = {{1041, 12180, 4}, {1062, 13240, 3}, {1327, 19800, 2}, {1483, 22458, 8}, {1900, 22458, 2}, {2112, 17000, 7}, {2345, 18125, 2}, {3210, 15623, 6}, {3600, 3200, 5}, {3601, 6500, 2}, {4724, 11970, 2}, {6217, 8900, 2}, {9280, 6200, 1}}; cout << "----------Household Survey----------" << endl << endl; cout << "ID Number\tAnnual Income\tHousehold Members" << endl; for (int i = 0; i < 13; ++i) cout << houses[i].idNumber << "\t\t" << houses[i].income << "\t\t" << houses[i].members << endl; //Average int sum = 0; double average = 0; for (int i = 0; i < 13; ++i) sum+=houses[i].income; average = sum/13; cout << "\n\n----------Average Income----------" << endl << endl; cout << "The average income is: " << average; cout << "The following households exceed the average income: "; if (houses.income > average) { cout << houses.idNumber << endl; } //Poverty Level int povertyLevel = 0; double povertyPercentage = 0; povertyLevel = 6500 + (750 * houses.members); if (houses.income < povertyLevel) { cout << houses.idNumber; } cout << "\n\n----------Poverty Level Statistics----------"; cout << "Poverty Level = 6500 + 750 * Household Members"; cout << "The following households are below poverty level: " << endl << houses.idNumber << endl; cout << povertyPercentage << "% of households are in poverty." << endl; //Run again? cout << "\nDo you want to run this program again? Y/N: "; cin >> again; cout << endl; } while (again == 'y' || again == 'Y'); return 0; }``````
it could be my compiler, but
`if (houses.income < povertyLevel)`

does not compile as houses in an array.

This:
`if (houses->income < povertyLevel)`

does compile, but I dont think that's what you want..

(using `houses->` like that is pointing to the first house in your array)
Last edited on
That does seem to allow it to compile.
Now I need a way to make the calculations work and to display the ID Numbers and percentage.

Here is the updated code...
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include using namespace std; struct Household { int idNumber; int income; int members; }; char again; int main() { do { //Displaying Survey Information Household houses[13] = {{1041, 12180, 4}, {1062, 13240, 3}, {1327, 19800, 2}, {1483, 22458, 8}, {1900, 22458, 2}, {2112, 17000, 7}, {2345, 18125, 2}, {3210, 15623, 6}, {3600, 3200, 5}, {3601, 6500, 2}, {4724, 11970, 2}, {6217, 8900, 2}, {9280, 6200, 1}}; cout << "----------Household Survey----------" << endl << endl; cout << "ID Number\tAnnual Income\tHousehold Members" << endl; for (int i = 0; i < 13; ++i) cout << houses[i].idNumber << "\t\t" << houses[i].income << "\t\t" << houses[i].members << endl; //Average int sum = 0; double average = 0; for (int i = 0; i < 13; ++i) sum+=houses[i].income; average = sum/13; cout << "\n\n----------Average Income----------" << endl << endl; cout << "The average income is: " << average << endl; cout << "The following households exceed the average income: "; if (houses->income > average) { cout << houses->idNumber << endl; } //Poverty Level int povertyLevel = 0; double povertyPercentage = 0; povertyLevel = 6500 + (750 * houses->members); if (houses->income < povertyLevel) { cout << houses->idNumber; } cout << "\n\n\n----------Poverty Level Statistics----------" << endl << endl; cout << "Poverty Level = 6500 + 750 * Household Members" << endl; cout << "The following households are below poverty level: " << endl << houses->idNumber << endl; cout << povertyPercentage << "% of households are in poverty." << endl; //Run again? cout << "\nDo you want to run this program again? Y/N: "; cin >> again; cout << endl; } while (again == 'y' || again == 'Y'); return 0; }``````
 That does seem to allow it to compile.

Yes, but it is really doing what you want it to do?

Do you understand what it's doing?
Last edited on
Topic archived. No new replies allowed.