Question

In: Computer Science

PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...

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

  • a four-digit integer identification number
  • the annual income for the household
  • the number of household members.

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

Solutions

Expert Solution

#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


Related Solutions

Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT