Write a program that uses structure to read employee is, name, age and salary. Sort them on the basis salary in data followed order. The program should display unsorted data followed by sorted data.
#include<stdio.h> #include<conio.h> typedef struct employee { int emp_id; char name[25]; int age; float salary; } emp; int main() { emp e[50],temp; int i,j,n; printf("How many employee?\n"); scanf("%d",&n); for(i=0; i<n; i++){ printf("\nEnter data: %d",i+1); printf("\nEmployee ID: "); scanf("%d",&e[i].emp_id); printf("Name: "); scanf("%s",&e[i].name); printf("Age: "); scanf("%d",&e[i].age); printf("Salary: "); scanf("%f",&e[i].salary); } printf("\nUnsorted data of employee: "); for(i=0;i<n;i++){ printf("\n%d.",e[i].emp_id); printf("\t\t%s",e[i].name); printf("\t\t%d",e[i].age); printf("\t\t%.2f",e[i].salary); } for(i=0; i<n-1; i++) { for(j=i+1; j<n; j++) { if(e[i].salary < e[j].salary) { temp = e[i]; e[i] = e[j]; e[j] = temp; } } } printf("\nSorted data is: "); for(i=0; i<n; i++){ printf("\n%d.",e[i].emp_id); printf("\t\t%s",e[i].name); printf("\t\t%d",e[i].age); printf("\t\t%.2f",e[i].salary); } getch(); return 0; }
Output
