P190. Program to store 5 student database which contains 3 information ( roll no – name – marks ) in unsorted order, ask the user for input in which way he/she needs the data to be sorted. 1 – Roll No 2 – Name 3 – Marks
#include<stdio.h>
#include<string.h>
struct
stu
{
int roll;
char name[10];
float t_marks;
};
void
main()
{
struct stu v[5],temp;
int i,j,ele,choice;
ele=sizeof(v)/sizeof(v[0]);
for(i=0;i<ele;i++)
{
printf("Enter roll number : ");
scanf("%d",&v[i].roll);
printf("Enter name : ");
scanf(" %s",v[i].name);
printf("Enter total marks : ");
scanf("%f",&v[i].t_marks);
printf("\n");
}
printf("\n");
printf("BEFORE SORTING\n");
for(i=0;i<ele;i++)
{
printf("%d %s %f
\n",v[i].roll,v[i].name,v[i].t_marks);
}
printf("\n");
while(1)
{
printf("Please select an option\n 1 -
Sort using Roll Number\n 2 - Sort using Name\n 3 - Sort using Total Marks\n
");
printf("Enter choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
for(i=0;i<ele-1;i++)
{
for(j=0;j<ele-i-1;j++)
{
if(v[j].roll>v[j+1].roll)
{
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
break;
case 2 :
for(i=0;i<ele-1;i++)
{
for(j=0;j<ele-i-1;j++)
{
if((strcmp(v[j].name,v[j+1].name)>0))
{
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
break;
case 3 :
for(i=0;i<ele-1;i++)
{
for(j=0;j<ele-i-1;j++)
{
if(v[j].t_marks >
v[j+1].t_marks)
{
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
break;
}
printf("\n");
printf("AFTER SORTING\n");
for(i=0;i<ele;i++)
{
printf("%d %s %f
\n",v[i].roll,v[i].name,v[i].t_marks);
}
printf("\n");
}
}
Comments
Post a Comment