Rather than using an array of struct-type elements, set up a linked list of struct-type nodes, where each node has the following fields or use Class using struct-type:
-a four-digit integer identification number
-the annual income for the household
-the number of household members
-a pointer field, linking this node to the next node
The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains data for one household as shown above (bold words).
a. To do the inputting, use a loop to set up one node at a time . Once a node is set up and made part of the linked list, ask the user if more nodes are to be added. If so. go through the loop at least one more time; if not, exit the loop and start with the next step.
b. Write code to count the number of households included in the survey and print (on the screen) a three-column table displaying the data.
c. Write code to calculate the average household income, and list the identification number and income of each household that exceeds the average.
d. Write code to Determine the percentage of households that have incomes below the poverty level. Compute the poverty level income using the formula p = $7000.00 + $850.00 × (m - 2) where m is the number of members of each household.
This formula shows that the poverty level depends on the number of family members, m, and that the poverty-level income increases as m gets larger.
Test your program on the following data.
Identification Number Annual Income Household Members
1041
12,180
4
1062
13,240
3
1327
19,800
2
1483 35,000 7
1900
17,000
2
2112
28,500
6
2345
15,623
2
3210
3,200
6
3600
6,500
5
3601
11,970
2
4724
8,900
3
6217
10,000
2
9280
6,200
1
Extra Credit: 10 points: if you use data files for inputting and outputting. 16 points: if you use functions for ALL the above tasks. Set up the functions to accept arguments.
#include <iostream>
using namespace std;
//definition of node
class Node
{
public:
int id;
int income;
int members;
Node *next;
};
//appending data of survey of every household to linked list
void append(Node** head_ref, int id1,int income1 ,int members1)
{
Node* new_node = new Node();
Node *last = *head_ref;
new_node->id = id1;
new_node->income = income1;
new_node->members = members1;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
//total number of surveys in which household is done
int count(Node* node)
{
int count = 0;
while (node != NULL)
{
count = count + (node->members);
node = node->next;
}
return count;
}
//return average income of household
int average_income(Node* node)
{
int count = 0;
int sum = 0;
int average = 0;
while (node != NULL)
{
count++;
sum = sum + node->income;
node = node->next;
}
average = sum/count;
return average;
}
//printing details of household having income higher than average
void print(Node* node)
{
int avg = average_income(node);
while (node != NULL)
{
if(avg<=node->income)
{
cout<<node->id<<" "<<node->income<<" "<<node->members<<endl;
}
node = node->next;
}
}
//return percentage below poverty
float percentage_below_poverty(Node* node)
{
float count = 0;
float below_poverty = 0;
while (node != NULL)
{
int minimum_income = 7000 + 850*(node->members-2);
if(node->income<=minimum_income)
below_poverty++;
node = node->next;
count++;
}
float percent = (below_poverty/count)*100;
return percent;
}
int main() {
int id,income,members;
id = 1;
Node* head = NULL;
int c =0;
while(id>0)
{
cin>>id;
if(id==-1)
break;
c++;
cin>>income>>members;
append(&head,id,income,members);
}
cout<<"Average income = "<<average_income(head)<<endl;
cout<<"Number of household = "<<c<<" and total household on whom survey is done = "<<count(head)<<endl;
cout<<"List of people having income greater than average :-"<<endl;
print(head);
cout<<"percentage below poverty = "<<percentage_below_poverty(head)<<endl;
}
INPUT AND OUTPUT
Get Answers For Free
Most questions answered within 1 hours.