Saturday, September 3, 2011

Sorting


/*  Write program that accept employee name,age,and salary into array and sort them in descending order of salary.  */

#include<stdio.h>

typedef struct employee
{
char ename[20];
int age;
float salary;
}employee;

int main()
{
int i,j,n;
float f;
employee e[15],temp;
printf("\nNo of employees:\n");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\n Enter details of %d employee \n",i+1);
printf("\nEnter name:");
scanf("%s",e[i].ename);
do
{
printf("\nEnter age in range of 18-80:");
scanf("%d",&e[i].age);
}while(e[i].age<18 || e[i].age>80) ;

      do{
printf("\nEnter salary:");
scanf("%f",&f);
}while(f<0);
e[i].salary=f;
}

printf("\nBefore sorting\n");
printf("\nNAME\tAGE\tSALARY");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%.2f",e[i].ename,e[i].age,e[i].salary);
}

// Sorting by using Bubble sort

for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(e[j].salary < e[j+1].salary)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
printf("\n\nAfter sorting\n");
printf("\nNAME\tAGE\tSALARY");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%.2f",e[i].ename,e[i].age,e[i].salary);
}

}

No comments:

Post a Comment