Anda di halaman 1dari 3

/*Accept Record of n students and sort it in descending order with respect to th

eir total marks */


#include<stdio.h>
//BluePrint For Structure Going to hold student records
typedef struct student
{
char name[50];
int roll,marks[3],total;
} student;
/* Function to update total marks in a record
It takes 2 parameters 1. array of records
2. index upto which records are to be updated */
void updatetotal(student records[], int n)
{
int i;
for(i=0;i<n;i++)
records[i].total = records[i].marks[0] + records[i].marks[1] + r
ecords[i].marks[2];
}
/* Function to print student records
It takes 2 parameters 1. array of records
2. index upto which records are to be printed */
void printlist(student records[], int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("--------------------------------------------------------------\n");
printf("Student no. %d \n ",i+1);
printf("Name :- %s\n ",records[i].name);
printf("Roll No:- :- %d\n ",records[i].roll);
printf("Marks :- \n ");
for(j=0;j<3;j++)
printf("\tSubject No. %d %3d \t",j+1,records[i].marks[j]
);
printf("\nTotal Marks :- %d\n\n\n",records[i].total);
}
}
/* Sorting Function: Returns index of minimum total in student records
It takes 2 parameters 1. array of records
2. index upto which records are to be checked */
int getminposition(student records[], int upper)
{
int min=0,i=0;
for(i=1;i<=upper;i++)
if (records[min].total>records[i].total)
min=i;
return min;
}

/* Sorting Function: Swap two records of an array


It takes 2 parameters 1. record 1 address
2. record 2 address */
void swap (student *r1, student *r2)
{
student temp=*r1;
*r1=*r2;
*r2=temp;
}
// Main Function Of Program
void main()
{
int n,i,j,min;
char dump;
printf("Enter the no of students ");
scanf("%d",&n);
if(n<1)
{
printf("Please Enter Valid Number");
return;
}
student records[200];
// Take Input Of student records
for(i=0;i<n;i++)
{
printf("\nEnter Records of Student %d\n",i+1);
scanf("%c",&dump);
printf("Enter your Name ");
scanf("%[^\n]s ",records[i].name);
printf("Enter your Roll No. ");
scanf("%d",&records[i].roll);
for(j=0;j<3;j++)
{
printf("Enter your Marks In Subject %d ",j+1);
scanf("%d",&records[i].marks[j]);
if(records[i].marks[j]<0)
{
printf("Invalid Marks Entered");
return;
}
}
}
updatetotal(records,n);
// Sorting
for(i=0;i<n;i++)
{
min=getminposition(records,n-1-i);
swap(&records[min],&records[n-1-i]);
}
printf("Here Are sorted records\n");

printlist(records,n);
}

Anda mungkin juga menyukai