Write a C program which will retrive and maipulate data from a company\'s payrol
ID: 662973 • Letter: W
Question
Write a C program which will retrive and maipulate data from a company's payroll database file named "payfile.txt" The data for each employee should be read into a struct containing the following field identifiers:
first, 7 character max. initial, 1 character max. last, 9 characters max. street, 16 characters max. city, 11 characters max. state, 2 characters max. zip, 5 characters max. age, integer. sex, 1 character max. tenure, integer. salary, double.
Each section should be designed within its own fucntion, passing parameters as necessary. The program should perform each of the following operations:
a) Read data for employees into an array of structures
b) output the contents of each structure into an easily read format, similar to the format of the input file.
c) output the first and last name of all men on the payroll.
d) output the first and last name of the highest paid woman on the payroll.
e) output the first and last name of the lowest paid man on the payroll.
f) output the average salary of all employess.
g) output the first and last name of all women earning less than the average.
h) output the ratio of the number of men above the average salary to the number of men below the average salary.
i) output the first and last name of all employess who make more than $35,000 per year, have been with the company for at least 5 years, and who are over 30 years old.
j) give a 10% raise to all employees who make less than $350.00 per week and output the first and last name and new salary for each employee who received the raise.
k) sort the structures according to zip codes and output the first and last name and zipcode for each employee.
Assume the following declaration:
#define MAX 100
Use the following function that grabs a substring.
void strsub(char buf[], char sub[], int start, int end){
int i,j;
for (j=0, i=start; i<=end; i++, j++){
sub[j] = buf[i];
}
sub[j] = '';
}
The contents of payfile.txt:
Please submit a sample screen output from your submission.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
FILE *csis;
typedef struct
{
char first [7];
char initial [1];
char last [9];
char street [16];
char city [11];
char state [2];
char zip [5];
int age;
char sex [1];
int tenure;
float salary;
} workers;
int main (void)
{
//PhamQuang 005174849
csis = fopen_s(&csis, "csis.txt", "w");
readEmployeeData();
outputContents();
outputFirstLastNameMen();
outputHighestPaidFemale();
outputLowestPaidMale();
outputAvgSalary();
outputFirstLastNameLessThanAvgSalary();
outputRatioMenAboveAvgSalary();
outputFirstLastNameEmployees$35000Salary5Years30YearsOld();
give10%RaiseToLessThan350PerWeek();
zipcodeFirstLastName();
return 0;
}
void strsub (char buf[], char sub[], int start, int end)
{
int i, j;
for (j = 0, i = start; i<= end; i++, j++);
sub[j] = buf[i];
sub[j] = '';
}
void readEmployeeData()
{
buf = fopen (buf, "payfile.txt", "r");
while (!feof(fp))
{
fgets(buf, MAX, fp);
strsub(buf, workers[i].first, 0, 6);
strsub(buf, workers[i].initial, 8, 8);
strsub(buf, workers[i].last, 10, 18);
strsub(buf, workers[i].street, 18, 34);
strsub(buf, workers[i].city, 34, 45);
strsub(buf, workers[i].state, 45, 47);
strsub(buf, workers[i].zip, 47, 52);
strsub(buf, workers[i].age, 52, 53);
strsub(buf, workers[i].sex, 53, 53);
strsub(buf, workers[i].tenure, 54, 54);
strsub(buf, workers[i].salary, 54, 60);
}
}
void outputContents()
{
printf buf;
}
void outputFirstLastNameMen()
{
if (workers[i].sex = M)
while (!feof(fp))
printf (workers[i].first);
printf (workers[i].last);
else
break;
}
void outputHighestPaidFemale()
{
if (workers[i].sex = F)&&(workers[i].salary = highest)
printf (workers[i].first);
printf (workers[i].last);
else
break;
}
void outputLowestPaidMale()
{
if (workers[i].sex = M)&&(workers[i].salary = lowest)
{
printf (workers[i].first);
printf (workers[i].last);
}
else
break;
}
void outputAvgSalary()
{
for (i = 1, i = MAX, i++)
{
totalSalary = totalSalary + workers[i].salary;
}
numberOfWorkers ++;
avgSalary = totalSalary / numberOfWorkers;
printf ("Average of employee's salary = " avgSalary);
}
void outputFirstLastNameLessThanAvgSalary()
{
if (workers[i].salary < avgSalary)
{
print (workers[i].first);
print (workers[i].last);
}
else
break;
}
void outputRatioMenAboveAvgSalary()
{
if ((workers[i].sex = M)&&(workers[i].salary > avgSalary)
{
menAboveAvgSalary ++;
}
else ((workers[i].sex = M)&&(workers[i].salary < avgSalary))
{
menBelowAvgSalary ++;
}
RatioMenAboveAvgSalary = menAboveAvgSalary / menBelowAvgSalary;
print ("Ratio of men with above and below avg. salary is: "RatioMenAboveAvgSalary;
}
void outputFirstLastNameEmployees$35000Salary5Years30YrsOld()
{
if ((workers[i].salary * 12 => 35000)&&(workers[i].tenure = 5)&&(workers[i].age > 30);
{
printf (workers[i].first)
printf (workers[i].last)
}
else
break;
}
void give10%RaiseToLesssThan$350PerWeek()
{
if (workers[i].salary < 350)
{
workers[i].salary = 0.10 * workers[i].salary;
printf (workers[i].first)
printf (workers[i].last)
}
else
break;
}
void zipcodeFirstLastName()
{
//sort structures according to zip code
printf (workers[i].first)
printf (workers[i].last)
printf (workers[i].zip)
}
#include <stdio.h>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.