This code takes id, name and marks of 3 subjects for 5 students from user base, calculates the total of 3 subjects per student, and then takes a criteria level with which to sort; level 1 sorting does sorting based on 1 criteria only, while level 2 will sort based on criteria 1 and if similar occurs (like if select level 1 criteria marks and if marks of two student are same) sorting of that pair will be done based on criteria 2, if either user have selected level 2 and criteria 2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int id;
int sub[3];
int total;
char name;
}s[5];
void sort1(struct student *[],int,int,int);
main()
{ int i,j,ch,c1,c2,t;
char choice;
for(i=0;i<5;i++)
{
s[i].total=0;
printf("Enter the id of s[%d]\n",i+1);
scanf("%d",&s[i].id);
printf("Enter the name:\n");
scanf("%s",&s[i].name);
for(j=0;j<3;j++)
{
printf("Enter the marks of sub[%d] of s[%d]\n",j+1,i+1);
scanf("%d",&s[i].sub[j]);
s[i].total+=s[i].sub[j];
}
}
struct student *p[5];
for(i=0;i<5;i++)
{
p[i]=&s[i];
}
printf("You wanna sort or not?\n");
scanf("%c",&choice);
if((choice == 'y') || (choice == 'Y'))
{
printf("Which level sorting 1 or 2 ?\n");
scanf("%d",&ch);
if(ch==2)
{
t=1;
printf("Criteria:\t1)By name\n2)By Id\n3)By total\nEnter level1 &level 2criteria:\n");
scanf("%d%d",&c1,&c2);
sort1(p,t,c1,c2);
}
else
{ t=0;
printf("Enter the level 1 criteria:\n");
scanf("%d",&c1);
sort1(p,t,c1,10);
}
}
}
void sort1(struct student *p[5],int t,int cri1,int cri2)
{
int i,j;
struct student *pt;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if((t==1)&&(cri1==1) && (strcmp(&p[i]->name,&p[i+j]->name)>0))
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
else if((t==1) &&(cri2==2)&& (strcmp(&p[i]->name,&p[i+j]->name)==0))
{
if(p[i]->id<p[i+j]->id)
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
}
else if((t==1)&&(cri2==3)&&(strcmp(&p[i]->name,&p[i+j]->name)==0))
{
if(p[i]->total<p[i+j]->total)
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
}
else if((t==0)&&(cri1==1) && (strcmp(&p[i]->name,&p[i+j]->name)>0))
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
else if((t==0) &&(cri1==2)&& (strcmp(&p[i]->name,&p[i+j]->name)>0))
{
if(p[i]->id<p[i+j]->id)
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
}
else if((t==0)&&(cri1==3)&&(strcmp(&p[i]->name,&p[i+j]->name)>0))
{
if(p[i]->total<p[i+j]->total)
{
pt=p[i];
p[i]=p[i+j];
p[i+j]=pt;
}
}
}
}
for(i=0;i<5;i++)
{
printf("Id\tName\tS1\tS2\tS3\tTotal\n\n%d\t%s\%d\t%d\t%d\t%d\n",p[i]->id,p[i]->name,p[i]->sub[0],p[i]->sub[1],p[i]->sub[2],p[i]->total);
}
}
char name; ... scanf("%s",&s[i].name);
is broken code. \$\endgroup\$