Question

Rather than using an array of struct-type elements, set up a linked list of struct-type nodes,...

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.

Homework Answers

Answer #1
#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. (Use C++ ) #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
Lists are members of a general category of abstract data types (ADTs) called containers (i.e., objects...
Lists are members of a general category of abstract data types (ADTs) called containers (i.e., objects whose purpose is to hold other objects). A list is a collection of items having the following defining characteristics: Homogeneity: All the items are of the same type. Linearity: Each item has a unique predecessor (except the first) and a unique successor (except the last). Variable Length: The number of items can vary over time. Order: Items may be ordered (i.e., as in a...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT