Program:
#include<stdio.h>
int n,i,j;
struct student
{
char name[20];
int sub1,sub2,sub3,total;
};
struct student st[100],temp;
void readlist()
{
for(i=0;i<n;i++)
{
printf("\nEnter the name of student:");
scanf("%s",st[i].name);
printf("Enter the marks in 3 subjects:");
scanf("%d%d%d",&st[i].sub1,&st[i].sub2,&st[i].sub3);
st[i].total=st[i].sub1+st[i].sub2+st[i].sub3;
}
}
void sortlist()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(st[j].total<st[j+1].total)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
}
void display()
{
for(i=0;i<n;i++)
{
printf("\nRank=%d",i+1);
printf("\nName:%s",st[i].name);
printf("\nTotal mark=%d\n",st[i].total);
}
}
void main()
{
struct student st[100],temp;
printf("Enter the number of students:");
scanf("%d",&n);
readlist();
sortlist();
printf("\n\t<<Sorted student Record>>");
display();
}
Output
nn@linuxmint ~ $ gcc c15.c
nn@linuxmint ~ $ ./a.out
Enter the number of students:3
Enter the name of student:varun
Enter the marks in 3 subjects:35
30
40
Enter the name of student:athira
Enter the marks in 3 subjects:39
45
40
Enter the name of student:afsal
Enter the marks in 3 subjects:40
45
49
<<Sorted student Record>>
Rank=1
Name:afsal
Total mark=134
Rank=2
Name:athira
Total mark=124
Rank=3
Name:varun
Total mark=105
nn@linuxmint ~ $
#include<stdio.h>
int n,i,j;
struct student
{
char name[20];
int sub1,sub2,sub3,total;
};
struct student st[100],temp;
void readlist()
{
for(i=0;i<n;i++)
{
printf("\nEnter the name of student:");
scanf("%s",st[i].name);
printf("Enter the marks in 3 subjects:");
scanf("%d%d%d",&st[i].sub1,&st[i].sub2,&st[i].sub3);
st[i].total=st[i].sub1+st[i].sub2+st[i].sub3;
}
}
void sortlist()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(st[j].total<st[j+1].total)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
}
void display()
{
for(i=0;i<n;i++)
{
printf("\nRank=%d",i+1);
printf("\nName:%s",st[i].name);
printf("\nTotal mark=%d\n",st[i].total);
}
}
void main()
{
struct student st[100],temp;
printf("Enter the number of students:");
scanf("%d",&n);
readlist();
sortlist();
printf("\n\t<<Sorted student Record>>");
display();
}
Output
nn@linuxmint ~ $ gcc c15.c
nn@linuxmint ~ $ ./a.out
Enter the number of students:3
Enter the name of student:varun
Enter the marks in 3 subjects:35
30
40
Enter the name of student:athira
Enter the marks in 3 subjects:39
45
40
Enter the name of student:afsal
Enter the marks in 3 subjects:40
45
49
<<Sorted student Record>>
Rank=1
Name:afsal
Total mark=134
Rank=2
Name:athira
Total mark=124
Rank=3
Name:varun
Total mark=105
nn@linuxmint ~ $