Teacher wants 'Separate each function with 2 blank lines' , did
i do it right? Would you check?
And also No global variables allowed. We must use function
prototype for this homework. The break, continue and goto C
commands are not allowed to be used. The break command is allowed
only as part of the switch statement.
Can you check and correct anything wrong?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 6
struct Employee
{
char name[30];
float sal;
float rate;
float raise;
float newSal;
//declare your struct data members here
};
void load (struct Employee e[], int n);
void ARate(struct Employee e[], int n);
void calcRaise(struct Employee e[], int n);
void sort(struct Employee e[], int n);
void total(struct Employee e[], int n,float *ts,float *tr,float *tn
);
void title();
void print(struct Employee e[], int n);
void printTotals(float ts,float tr, float tn);
void savetext(struct Employee e[], int n);
void gettext(struct Employee e[], int n);
void savebn(struct Employee e[], int n);
void getbn(struct Employee e[], int n);
//function prototype here
int main()
{
//declaration of variables
struct Employee e[SIZE];
float ts = 0, tr = 0, tn = 0;
//load data into struct, calculate raises, salaries
and new salaries and print the
//original array of struct
load(e,SIZE);
ARate(e,SIZE);
calcRaise(e,SIZE);
total(e,SIZE,&ts,&tr,&tn);
printf("Original Array of Structure before
Sorting\n\n");
title();
print(e,SIZE);
printTotals(ts,tr,tn);
//sort the struct of array and print the sorted
struct of array
sort(e,SIZE);
total(e,SIZE,&ts,&tr,&tn);
printf("Array of Structure after Sorting\n\n");
title();
print(e,SIZE);
printTotals(ts,tr,tn);
//**************Section for text
files*****************
printf("\n\n");
printf("From Save Text file\n\n");
title();
//save the struct of array to a text file
savetext(e,SIZE);
//retrieve the text file and print the data
gettext(e,SIZE);
print(e,SIZE);
total(e,SIZE,&ts,&tr,&tn);
printTotals(ts,tr,tn);
//**************Section for binary
files*****************
printf("\n\n");
printf("From Save Binary file\n\n");
title();
//save struct of array to a binary file
savebn(e,SIZE);
//retrieve data from binary file and print the
data
getbn(e,SIZE);
print(e,SIZE);
total(e,SIZE,&ts,&tr,&tn);
printTotals(ts,tr,tn);
return 0;
}
void load (struct Employee e[], int n)
{
for (int i = 0; i< n; i++)
{
printf("Enter the employee's name: ");
scanf("%s", e.name);
printf("Enter Salary: ");
scanf("%f", &e.sal);
e.rate = 0.0f;
}
//let user enter employee’s name and salary
}
void ARate(struct Employee e[], int n)
{
for (int i = 0; i< n; i++)
{
if (e.sal > 0 && e.sal < 30000)
{
e.rate = 7.0f;
}
else if (e.sal >= 30000 && e.sal <=
40000)
{
e.rate = 5.5f;
}
else
{
e.rate = 4.0f;
}
}
//assign each employee rate to the struct’s rate data member here
by checking salary range
//for example
// if(e[i].sal >= 0.00 && e[i].sal < 30000.00)
// e[i].rate = 7.00;
}
void calcRaise(struct Employee e[], int n)
{
for (int i = 0; i < n; i++)
{
e[i].raise = (e[i].sal*e[i].rate)/100;
e[i].newSal = e[i].sal + e[i].raise;
}
//calculate the raise and new salary for each employee and store
into the
//struct’s raise and new salary data members
// raise = salary * rate/100
// new salary = salary + raise
}
void sort(struct Employee e[], int n)
{
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (strcmp(e[j].name, e[j + 1].name) > 0)
{
struct Employee temp = e[j];
e[j] = e[j + 1];
e[j + 1] = temp;
}
}
}
//sort the struct in ascending order
}
void total(struct Employee e[], int n,float *ts,float *tr,float
*tn )
{
*ts = 0;
*tr = 0;
*tn = 0;
for (int i = 0; i < n; i++)
{
*ts = *ts +e[i].sal;
*tr = *tr+ e[i].raise;
*tn = *tn + e[i].newSal;
}
//ts = total salary(sum the salaries of all 6 employees)
//tr = total raise(sum the raises of all 6 employees)
//tn = total new salary(sum of new salaries of all 6 employees)
}
void title()
{
printf("\t\t\tCalculation of Salary Raises\n\n");
printf("Employee\t"); printf("Salary\t\t");
printf("Rate %\t"); printf(" Raise\t\t"); printf("New
Salary\n\n");
}
void print(struct Employee e[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf("%s\t\t", e[i].name);
printf("%10.2f\t", e[i].sal); printf("%4.2f\t", e[i].rate);
printf("%8.2f\t", e[i].raise);
printf("%8.2f\t\n", e[i].newSal);
}
}
void printTotals(float ts,float tr, float tn)
{
printf("Total\t\t"); printf("%10.2f\t\t", ts);
printf("%8.2f\t", tr); printf("%8.2f\n\n", tn);
}
void savetext(struct Employee e[], int n)
{
FILE *outfile;
outfile = fopen ("employees.txt", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
for (int i = 0; i< n; i++)
{
fwrite (&e[i], sizeof(struct Employee), 1, outfile);
}
fclose (outfile);
//save the struct to text file employees.txt
}
void gettext(struct Employee e[], int n)
{
FILE *infile;
struct Employee input;
infile = fopen ("employees.txt", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
for (int i = 0; i< n; i++)
{
if(fread(&input, sizeof(struct Employee), 1, infile) ==
1)
e[i] = input;
}
//retrieve the data from employees.txt file
}
void savebn(struct Employee e[], int n)
{
FILE *outfile;
outfile = fopen ("employees.bin", "wb");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
for (int i = 0; i< n; i++)
{
fwrite (&e[i], sizeof(struct Employee), 1, outfile);
}
fclose (outfile);
//save the struct to a binary file employees.bin
}
void getbn(struct Employee e[], int n)
{
FILE *infile;
struct Employee input;
infile = fopen ("employees.bin", "rb");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
for (int i = 0; i< n; i++)
{
if(fread(&input, sizeof(struct Employee), 1, infile) ==
1)
e[i] = input;
}
fclose (infile);
//retrieve the data from the employees.bin file
}
Teacher wants 'Separate each function with 2 blank lines' , did i do it right? Would you check? And also No global varia
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am