Actually, I am trying to upload the picture but it dosen\'t work as well. In thi
ID: 3878080 • Letter: A
Question
Actually, I am trying to upload the picture but it dosen't work as well.
In this programm i should open a file name lab2data.txt and it contains : first_name, last_name, salary, gender.
The language should be either Eclipse or Dev C++.
This is the explanation for this programm:
We are given a data file from a company containing all their employee names, salary, and gender. We are to process the data and display gender and salary statistics.
File format (each field separated by a tab):
• First row is a header row
• All other rows:
• first_name last_name salary gender
Safe assumptions to make No spaces will be in the file, only tabs. (No last names have more than one word)
Gender will be only Female or Male
You do not need to STORE the entire file in memory, process each line and forget the details that do not matter, such as employee’s name.
Your code should work for any data that follows the formatting rules File may contain MORE then 1000 records, perhaps millions
File name may be hard coded, however, do not add any path info After reading in and processing the data:
• Output the number of employees found, the number of females, and the number of males. • Output the average total salary, average female salary, and the average male salary.
Finally, the output would be:
Total Persons: 1000
Total Females: 486
Total Males: 514
Average Salary for all: $105230
Average Salary for Females: $108097
Average Salary for Males: $102519
Explanation / Answer
Wrote the code using in C in DevC++. Header condition only wont work except that below code met with all conditions of question.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
int i =0, salary, fcount = 0, salary1 =0,mcount=0,fsal=0,msal=0, Tsalary=0, AvgSAL, AvgFSAL, AvgMSAL;
char isquared[10], name[50], lname[50],gender[50];
/* open the file */
fp = fopen("eg.txt", "r");
if (fp == NULL) {
printf("I couldn't open the file ");
exit(0);
}
while (!feof(fp) )
{
fscanf(fp, "%s %s %d %s", &name, &lname, &salary, &gender);
if(gender =='female')
{
fcount = fount+1;
fsal = salary+fsal;
}
else{
msal = salary+msal;
mcount = mcount+1;
}
Tsalary = Tsalary+salary
i++;
}
AvgSAL= Tsalary/i;
AvgFSAL = fsal/fcount;
AvgMSAL = msal/fcount;
printf("Total persons are: %d", i);
printf("Total females are: %d", fcount);
printf("Total males are: %d", mcount);
printf("AVG salary for females: %d", AvgFSAL);
printf("AVG salary for males: %d", AvgFSAL);
printf("AVG salary for all: %d", AvgSAL);
/* close the file */
fclose(fp);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.