Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The results of a survey of the households in your township have been made availa

ID: 3695838 • Letter: T

Question

The results of a survey of the households in your township have been made available. Each record contains data for one household, including a four-digit integer identification number, the annual income for the household, and the number of members of the household. You may assume no more than 25 households were surveyed. Define a structure type named household_t to store the record for one family

You need to write two separate program files. The description of each program file is as follow:

Program 1

You need to write a program to create a binary data file of the household data. Data should be requested from the keyboard and written to the data file.

Program 2

You need to write another program to read the household data from a binary data file. The program also calculates the average income, the household exceeding the average income, and the percentage of households having incomes below the poverty level. You need to use the same structure type household_t defined in the Program 1 above. Declare an array of the structures household_t [ 25 ] for up to 25 households.

The required user-defined functions are as follow:

Write a function to read the data from the data file created in Program 1 and store them into the array of structures.

Write a function to print the array of structures in a three-column table with the appropriate column headings.

Write a function to calculate the average income.

Write a function to find and display a listing of the identification number and income for each household exceeding the average income. The results must be in a two-column table with the appropriate column headings.

Write a function to determine the percentage of households having incomes below the poverty level. The poverty level income may be computed using the formula:

                        P = $7500.00 + $950.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 the poverty level increases as m gets larger. The binary data file should be named household.dat file.

Explanation / Answer


#include <stdio.h>           /* input, output functions */
#define MAX_ARR_SIZE   25    /* Size of array holding household structs */

typedef struct {
   int id;           /* Holds household ID*/
   int income;       /* Holds household income */
   int numMembers;   /* Holds number of household members */
} household_t;

/* Function Prototyes */
int fillArray(household_t houseList[], FILE *infile);
void printArray(int count, household_t houseList[]);
double averageIncome(int count, household_t houseList[]);
void aboveAverage(int count, double average, household_t houseList[]);
double poverty(int count, household_t houseList[]);

int
main(void) {
   int count;                           /* Holds number of household_t's in houseList minus 1 */
   household_t houseList[MAX_ARR_SIZE];   /* Array of household_t structs */
   double average;                           /* Holds the average income of all valid household_t's */
   double povertyPercent;                   /* Holds percentage of households below poverty */
   FILE *infile;                           /* The file to input from */
  
   /* Open the file */
   infile = fopen("household.dat", "rb");
  
   /* Fill the array with data from file */
   count = fillArray(houseList, infile);
   if (count == -1) {
       printf("Error: File is empty");
       return (0);
   }
  
   /* Print the array */
   printArray(count, houseList);
  
   /* Calculate the average income and print to screen */
   average = averageIncome(count, houseList);
   printf(" The Average Household Income is $%8.2f ", average);
  
   /* List the households above the average income */
   aboveAverage(count, average, houseList);
  
   /* Calculate the percentage of houses under the poverty line and output */
   povertyPercent = poverty(count, houseList);
   printf(" The Percentage of Houses Below the Poverty Level is %%%6.2f", (povertyPercent * 100));
  
   /* close the file and end the program */
   fclose(infile);
   return (0);
}

/* Reads data from infile and saves to array of household_t structs */
/* Returns int containing the number of files read in */
int
fillArray(household_t houseList[], FILE *infile) {
   int inputData;   /* Holds data from input to pass to struct */
   int status = 1; /* Used to determine if at end of file */
   int i = -1;
  
   /* Loop through reading data from file, saving to structs */
   /* Uses i to increment through array, and passes to main as count */
   status = fread(&inputData, sizeof (int), 1, infile);
   while (status != 0) {
       i++;
       houseList[i].id = inputData;
       fread(&houseList[i].income, sizeof (int), 1, infile);
       fread(&houseList[i].numMembers, sizeof (int), 1, infile);
       status = fread(&inputData, sizeof (int), 1, infile);
   }
  
   return (i);
}

/* Prints the contents of the array to the screen */
void
printArray(int count, household_t houseList[]) {
   /* Print header */
   printf("Identification Number Annual Income Household Members ");
  
   /* Loop through array and print each record */
   for (int i = 0; i <= count; i++) {
       printf(" %4d $%6d %d ", houseList[i].id, houseList[i].income, houseList[i].numMembers);
   }
   printf(" ");
   return (0);
}

/* Calculates the average income among households in array */
double
averageIncome(int count, household_t houseList[]) {
   int total = 0;    /* Holds running total of all households */
   double average;   /* holds calculated average income */
  
   /* Loop through array and increment total for each record */
   for (int i = 0; i <= count; i++) {
       total += houseList[i].income;
   }
  
   /* calculate average, casting total to a double and adding one to count (which is 1 less than the actual count) */
   average = (double)total / (count + 1);
  
   return (average);
}

/* Lists the houses above the average income */
void
aboveAverage(int count, double average, household_t houseList[]) {
   /* Print headers */
   printf("Houses with above average income: ");
   printf("Identification Number Annual Income ");
  
   /* Loop through array and print each record with income > average */
   for (int i = 0; i <= count; i++) {
       if (houseList[i].income > average) {
           printf(" %4d $%6d ", houseList[i].id, houseList[i].income);
       }
   }
   printf(" ");
   return (0);
}

/* Calculates the percentage of households under the poverty level */
double
poverty(int count, household_t houseList[]) {
   int numPoverty = 0;   /* Holds number of households under poverty level */
   double povertyPercent; /* Holds percentage of poverty */
  
   /* Loop through array, increment numPoverty if record has income below calculated poverty level */
   for (int i = 0; i <= count; i++) {
       double thisPoverty;
       thisPoverty = 7500 + (950 * (houseList[i].numMembers - 2));
       if ((double)houseList[i].income < thisPoverty) {
           numPoverty++;
       }
   }
  
   /* Calculate poverty level and return */
   povertyPercent = (double)numPoverty / (count + 1);
  
   return (povertyPercent);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote