c++ - Lab Assignment 8:Due: April 15 Write a program that reads the following in
ID: 3706197 • Letter: C
Question
c++
- Lab Assignment 8:Due: April 15 Write a program that reads the following income amounts from a file named inc.txt (in your z drive in C+t folder). The program should count number of male and female employees , compute and display the average income for each gender, as well as the display the count of males and females on the screen. Expected output below: unt: ender: M Amount: 2600o.oo ender: M Amount: 420o0. 0O ender: F Amount: 53000. o0 ender:F Amount: 4 6000. 00 ender:M Amount: 49000. 00 or 3 females, the average income is 42333.33 or 3 males. the averageincome is 39000.00 ress any key to continue . Income data should be in classified as follows: F 28000Explanation / Answer
Please paste below code. I am not using Windows on my Laptop, so I couldn't check file path for inc.txt. In case, you are getting the message like " Error: Can't open the file named inc.txt." , then please change line #29 to " income_data.open("Z:\C++inc.txt") " . If the problem persists after the change, please do comment. I will find another solution.
// code starts here
#include <iostream>
#include <cstdlib>
// Library to extract data from a file
#include <fstream>
using namespace std;
//MACRO Definition for the size of gender and income data array
#define ARR_SIZE 1000
int main() {
// Declaration of Arrays that will contain gender and income details of the employes which we will get from the file
string gender_arr[ARR_SIZE];
float income_arr[ARR_SIZE];
// Declaration and Initialization of variables for No. of Female and Male Employees
int no_of_female_emp = 0;
int no_of_male_emp = 0;
// Declaration and Initialization of variables for total sum of all females and males employees
float sum_of_females_income = 0;
float sum_of_males_income = 0;
// Variable for total rows in the file
int ind;
// To read data from a file into variables, first we need to create an input file stream object.
// In our case, I have created an input file stream object called "income_data".
ifstream income_data;
// Now, we will use income_data object to open the file which is having income data of all employees.
// This can be done by the below instruction income_data.open("path/to/inc.txt").
// I have the file path which is mentioned in the question. You can change it by changing the below line.
income_data.open("Z:/C++/inc.txt");
if ( !income_data ) {
cout << "Error: Can't open the file named inc.txt." << endl;
exit(0);
}
// loop to store gender and income details of all employees in gender array and income array from the file.
// As it is mentioned in the question that data will be like this:
/* F 28000
M 26000
M 42000
F 53000
F 46000
M 49000 */
// So, 1st column will be extracted as string and 2nd column as integer. All details will be stored in gender array and income array.
for(ind = 0; income_data; ind++){
income_data >> gender_arr[ind];
income_data >> income_arr[ind];
}
for(int j = 0; j < ind-1; j++) {
// Display all gender and income details
cout << "gender: " << gender_arr[j] << " " << "Amount: " << income_arr[j] << endl;
// Check if gender is "Female"
if(gender_arr[j] == "F"){
// count for all female employees
no_of_female_emp++;
// sum of income of all female employees
sum_of_females_income += income_arr[j];
}
// check if gender is "Male"
if(gender_arr[j] == "M"){
// count for all male employees
no_of_male_emp++;
// sum of income of all male employees
sum_of_males_income += income_arr[j];
}
}
// Calculate Average Income of all Female Employees
float avg_females_income = sum_of_females_income/no_of_female_emp;
// Calculate Average Income of all male Employees
float avg_males_income = sum_of_males_income/no_of_male_emp;
// Display count and Average Income of all Female Employees
cout << "For " << no_of_female_emp << " females, the avergae income is " << avg_females_income << endl;
// Display count and Average Income of all Male Employees
cout << "For " << no_of_male_emp << " males, the avergae income is " << avg_males_income << endl;
return 0;
}
//code ends here
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.