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...
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
#include <iostream>
#include <iomanip>
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...
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
#include <iostream>
#include <iomanip>
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.