------------------------------------------------------------------------------------------------------------
Write a program that reads information about 20 employees and store it in an array of structures. The structure of each employee should contain: name (does not exceed 20 characters), ID (int), salary (double) between 200 and 4000, and address which belongs to one of the following {A, B, C, D, E}. The program should then output the following:
1.the average of employee salaries
2.the names of employees whose salaries are above the average
3.compute the employees total tax amount, given that tax amount is 10% of the salary for salaries above 1000 and 5% for salaries below or equal to 1000
4.the list of employees and their information sorted in alphabetical order of name
5.print a table that shows the distribution of salaries over the 5 cities as follows:
Below 1000Between 1000 and 2000Between 2000 and 3000Between 3000 and 4000
A
B
C
D
E
Loading
VulpesPosted May 3, 2013, 6:03 AM
#include
#include
struct employee
{
char name[21];
int ID;
double salary;
char address;
};
void sort_employees(struct employee emp[], int start, int n)
{
int i, j;
struct employee temp;
for(i = 1 + start; i < start + n; i++)
{
for(j = start + n - 1; j >= i; j--)
{
if(strcmp(emp[j - 1].name, emp[j].name) > 0)
{
temp = emp[j - 1];
emp[j - 1] = emp[j];
emp[j] = temp;
}
}
}
}
#define N 20 /* allow for using a smaller number than 20 for testing puposes */
int main()
{
int i;
char c;
struct employee employees[N];
double total_salary = 0.0, avg_salary;
double total_tax = 0.0;
double range1, range2, range3, range4;
printf("Enter details for 20 employees");
for(i = 0; i < N; i++)
{
printf("\n\n Employee %d\n\n", i + 1);
printf(" Name (max 20 characters) : ");
gets(employees[i].name);
printf(" ID : ");
employees[i].ID = 0;
scanf("%d", &employees[i].ID);
employees[i].salary = 200.0;
do
{
printf(" Salary (200 to 4000) : ");
scanf("%lf", &employees[i].salary);
getchar();
}
while(employees[i].salary < 200 || employees[i].salary > 4000);
total_salary += employees[i].salary;
if (employees[i].salary > 1000)
{
total_tax += 0.1 * employees[i].salary;
}
else
{
total_tax += 0.05 * employees[i].salary;
}
employees[i].address ='A';
do
{
printf(" Address (A,B,C, D or E) : ");
scanf("%c", &employees[i].address);
getchar();
employees[i].address = toupper(employees[i].address);
}
while(employees[i].address < 'A' || employees[i].address > 'E');
}
avg_salary = total_salary/N;
printf("\nThe average salary is %.2f\n\n", avg_salary);
printf("\nThe names of employees whose salaries are above the average are\n\n");
for(i = 0; i < N; i++)
{
if (employees[i].salary > avg_salary)
{
printf(" %s (%.2f)\n", employees[i].name, employees[i].salary);
}
}
printf("\nThe employees' total tax amount is %.2f\n", total_tax);
sort_employees(employees, 0, N);
printf("\nAfter sorting into name order the employee details are\n\n");
printf("\n%-20s %4s %7s %s", "Name", "ID", "Salary", "Address");
printf("\n%-20s %4s %7s %s", "----", "--", "------", "-------");
for(i = 0; i < N; i++)
{
printf("\n%-20s %4d %7.2f %c", employees[i].name, employees[i].ID, employees[i].salary, employees[i].address);
}
printf("\n\n\nDistribution of salaries by address\n\n");
printf("%7s %10s %11s %11s %11s\n", "Address", "[400-1000)", "[1000-2000)", "[2000-3000)", "[3000-4000]");
printf("%7s %10s %11s %11s %11s\n", "-------", "----------", "-----------", "-----------", "-----------");
for(c = 'A'; c <= 'E'; c++)
{
range1 = range2 = range3 = range4 = 0.0;
for(i = 0; i < N; i++)
{
if(employees[i].address == c)
{
if(employees[i].salary < 1000)
{
range1 += employees[i].salary;
}
else if (employees[i].salary < 2000)
{
range2 += employees[i].salary;
}
else if (employees[i].salary < 3000)
{
range3 += employees[i].salary;
}
else
{
range4 += employees[i].salary;
}
}
}
printf("%7c %10.2f %11.2f %11.2f %10.2f\n", c, range1, range2, range3, range4);
}
return 0;
}