In: Computer Science
PLEASE DO IN C++ AND USE REPL TO WRITE CODE
The following problem statement is based on a problem in the C++ text by Friedman & Koffman:
The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including
Assuming that no more than 25 households were surveyed, write a program to:
A. read the survey results into an array of structures or objects. The inputting should continue until negative values (sentinel values) are encountered or 25 records are obtained (size of the array). In other words, the array may or may not fill up.
B. Count the number of households included in the survey and print (on the screen) a three-column table displaying the data. Display appropriate headers, prior to displaying the contents of the array of structs.
C. Calculate and display the average household income. Display the average household income using an appropriate message, and list the identification number and income of each household that exceeds the average, using a two-column table. Display an appropriate message prior to displaying the two-column table.
D. 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.
E. Display the percentage of households that have incomes below the poverty level, using an appropriate message, once it's been computed.
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
-1 -1 -1
#include <iostream>
#include <string> //for using string class
#include <fstream> //for file input
#include <stdio.h>
#include <stdlib.h>
#include <sstream> //for converting of string to int
using namespace std;
int main() {
const int SIZE=25; //number of records to read
const int term=-1;
int numOfHousehold=0; //counter to store number of household
struct Records {
int idenNum;
string annualIncome;
int houseHoldMem;
}record[SIZE]; //record structure
ifstream in("Data.txt"); //opening if input stream
//check if input stream is properly opened
if (!in){
cerr << "File can't be opened! " << endl;
system("PAUSE");
exit(1);
}
//to get the first header line
string str;
getline(in,str);
//loop to read all records until -1 or 25 records are read
for (int i=0; i < SIZE; i++){
in >> record[i].idenNum >> record[i].annualIncome
>>record[i].houseHoldMem;
if (record[i].idenNum == term)
break;
else
//if record read increase the counter
numOfHousehold++;
}
//output the number of households
cout<<"The number of Households surveyed are "<<numOfHousehold<<endl;
cout << str<<endl;
//print the data which is read
for (int i=0;i< SIZE;i++) {
if (record[i].idenNum == term)
break;
cout << record[i].idenNum<<"\t\t\t\t\t";
cout << record[i].annualIncome<<"\t\t\t\t\t";
cout << record[i].houseHoldMem<<"\t\t\t\t\t";
cout<<endl;
}
//removinf comma from annual income column
for(int i=0;i<numOfHousehold;i++)
{
string str1=record[i].annualIncome;
int len;
for (int j = 0, len = str1.size(); j < len; j++)
{
if (str1[j]==',')
{
str1.erase(j--, 1);
len = str1.size();
}
}
record[i].annualIncome=str1;
}
//calculate the total annual income
int totalSum=0;
for(int i=0;i<numOfHousehold;i++){
//for converting string to int
stringstream geek(record[i].annualIncome);
int x = 0;
geek >> x;
totalSum=totalSum+x;
}
//average calculation and printing
float avg=(float)totalSum/numOfHousehold;
cout<<"The average income of HouseHolds is "<<avg<<endl;
cout<<"Identification Number"<<" "<<"Annual Income"<<endl;
//printing households who have annual income
//greater than average annual income
for(int i=0;i<numOfHousehold;i++){
stringstream geek(record[i].annualIncome);
float x = 0;
geek >> x;
if(x>avg)
{
cout<<record[i].idenNum<<"\t\t\t\t\t";
cout<<record[i].annualIncome<<endl;
}
}
//calculate number of households below poverty
//using the formula given in question
int numBelowPoverty=0;
for(int i=0;i<numOfHousehold;i++){
int povertyLevel=7000+850*(record[i].houseHoldMem-2);
stringstream geek(record[i].annualIncome);
int x = 0;
geek >> x;
if(x<povertyLevel)
{
numBelowPoverty++;
}
}
//number of household below poverty percentage
cout<<"The number of Households Below Poverty is "<<numBelowPoverty<<endl;
float percent=((float)numBelowPoverty/numOfHousehold)*100;
cout<<"The percentage of Households below poverty level are "<<percent<<"%";
}
Text File
Output