C programme to use structure and array together for sorting
Download the .c file at the End of the post.
/* Create a structure student having data members
enroll,name,marks obtained in 5 subjects. Get data of 5 student from user
and calculate average of 5 subjects of each student and
sort the data based on average in the ascending order and print result. */
#include<conio.h>
#include<stdio.h>
void main()
{
struct Student
{
int enroll;
char sname[50];
int m[5];
int avg;
}s[5],temp;
int i,j,sum=0;
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter enrollment number ");
scanf("%d",& s[i].enroll);
printf("\n Enter Student's name ");
scanf("%s",s[i].sname);
printf("\n Enter marks of 5 subjects one by one");
for(j=0;j<5;j++)
{
scanf("%d",&s[i].m[j]);
}
}
for(i=0;i<5;i++)
{
sum=0;
for(j=0;j<5;j++)
{
sum=sum+s[i].m[j];
}
s[i].avg=sum/5;
}
//for sorting
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(s[i].avg>s[j].avg)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("\n\n\nDetails of Students after sorting acc. to avg\n");
for(i=0;i<5;i++)
{
printf("\nRank %d",i+1);
printf("\nEnrollment Number:-> %d", s[i].enroll);
printf("\nStudent's Name:-> %s",s[i].sname);
printf("\n Marks of 5 subjects");
for(j=0;j<5;j++)
{
printf("\t%d",s[i].m[j]);
}
}
getch();
}
Download the .c file of this programme - CLICK HERE
0 comments:
Post a Comment