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

CSIT 575-Programming Fundamentals for Computer Science Lab #SA ObjectIves TO lea

ID: 3570726 • Letter: C

Question

CSIT 575-Programming Fundamentals for Computer Science Lab #SA ObjectIves TO learn to code conale and rwn a prograni contar. ai array 01 stnes. Assltrsm,nt Plan and code a modidar p1am utilsl,ng an array of uctwes for the kil..wsg problem The resits of a survey of the households es Wooand Is have been made ewedae. Each record contains a four diaracter identification code, the irwixal vne loe the household, and the rs.nber of members of the household Wntea program that inputs tht wdoemawn froni a e and then prs?scvwi a MENU of options avadable to tIse us.?. The optiore should asdude A, Print all of the input data lh appropriate cohaivi headesgi B Calculate the averqe household escome and nt the idenWscaoon codes and escome of each household wfsose Income n greater than the averqe. C. Desarmase the percentage of households hivuig an escome below tIse poverty buel The formula for dxt.?niwsing the poverty level is P 5800000.550000 ? (M? li where Mn the numbe of members of the household D. Preit all of the resut data sorted by household escame. E. Calculate aid prwit the median household escome. The medias is the m.ddie value of a sorted 1st. lIthe lot contains an even nun*Cr of eixtrses tIse rnen is the average of the t iwddle valueL Input tht record intormation .sto ais array of sUuctwes Use tfsq dato Me below. Diplay a weil-formatt menu of choices tot the user SUitE TO ADEQUATELY TEST CACtI CHOICE OIl VOUK LMNU Do NOT test the options in order. Create the data file below its text editor or Notepad Data File WHVC 3400000 S AMA 10500.00 8 BURR 23500.00 2 CCCC 1500000 4 DATA 1000.00 3 EEEE 3600000 S FACE $500.00 4 GA?II 2500000 1 HILO 300000 1 IURY 10000000 5 KNEL 80000 4 UST 41000.00 3 MEtAN 5000.00 2 PORS 18000.00 2 RCLM 27500.00 4 SOLD 2210000 2 Output the appropriate mlormjtinn loi tite user Print out each of thes, options to a data 81 lo hand as to your Instructor Turn In Top down desqn with parameters program listing and program output. Tiwn ai. data fibs used In the program.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int size = 1000;
int count = 0;

struct house{
   string id;
   double income;
   int members;
};

void swap(house *arr, int i, int j){
   house temp = arr[i];
   arr[i] = arr[j];
   arr[j] = temp;
}

void bubbleSort(house *arr){
   for(int i = 0; i < count - 1; ++i){
       for(int j = 0; j < count - i - 1; ++j){
           if(arr[j].income > arr[j + 1].income){
               swap(arr, j, j + 1);
           }
       }
   }
}

void readData(ifstream &file, house *arr){
   string val;
   double in;
   int num;
   while(file >> val){
       house temp;
       temp.id = val;
       file >> in;
       temp.income = in;
       file >> num;
       temp.members = num;
       arr[count++] = temp;
   }
}

void printData(house *arr, double avg = 0.0){
   cout << "******************************************" << endl;
   cout << "ID Income Members " << endl << endl;
   cout << "******************************************" << endl;
   for(int i = 0; i < count; ++i){
       if((double)(arr[i].income) > avg){
           cout << arr[i].id << " " << arr[i].income << " " << arr[i].members << endl;
       }
   }
   cout << endl << endl;
}

double getAvg(house *arr){
   double avg = 0.0;
   for(int i = 0; i < count; ++i){
       avg += arr[i].income;
   }
   return avg / count;
}

bool povertyLine(house temp){
   if(temp.income < 8000 + (500 * (temp.members - 2))){
       return true;
   }
   return false;
}

double getPercentage(house *arr){
   int total = 0;
   for(int i = 0; i < count; ++i){
       if(povertyLine(arr[i])){
           total++;
       }
   }
   return (total / (double)count) * 100;
}

double getMedian(house *arr){
   if(count % 2 == 0){
       return ((arr[count / 2].income) + arr[(count / 2) - 1].income) / 2.0;
   }
   else{
       return arr[count / 2].income;
   }
}

int main(){
   ifstream file;
   file.open("houses.txt");
   house *arr = new house[size];
   if(file.is_open()){
       readData(file, arr);  
   }
   else{
       cout << "Cannot open houses.txt" << endl;
   }
   printData(arr);
   double avg = getAvg(arr);
   cout << "Average Income is: " << avg << endl << endl;
   printData(arr, avg);
   cout << "Percentage of having income less than poverty Line is " << getPercentage(arr) << endl;
   bubbleSort(arr);
   printData(arr);
   cout << "Median household Income is " << getMedian(arr) << endl << endl;
   return 0;
}