Thursday, 21 June 2012

write a c program to create student having the field roll_no,stud_name, marks1,marks2,marks3.calculate the total and average of marks and arrange the record in descending order of marks


/*write a c program to create student having the field roll_no,stud_name,
marks1,marks2,marks3.calculate the total and average of marks and arrange
the record in descending order of marks*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct stud
{
int rollno;
int m1;
int m2;
int m3;
int total;
float avg;
char stud_name[20];
};
struct stud s[3];
int i,j,p,temp=0,m,total,m1=0,m2=0,m3=0,rno=0,tot=0;
float avg;
char n[30];
clrscr();
for(i=0,total=0,avg=0;i<3;i++)
{
printf("\nenter the roll no :");
scanf("%d",&s[i].rollno);
printf("\nenter the name :");
flushall();
scanf("%s",&s[i].stud_name);
printf("\nenter the marks 1 :");
scanf("%d",&s[i].m1);
printf("\nenter the marks 2 :");
scanf("%d",&s[i].m2);
printf("\neneter the marks3 :");
scanf("%d",&s[i].m3);
total=s[i].m1+s[i].m2+s[i].m3;
avg=total/3;
s[i].avg=avg;
s[i].total=total;
}
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++)
{
if(s[i].avg<s[j].avg)
{
temp=s[i].avg;
s[i].avg=s[j].avg;
s[j].avg=temp;
strcpy(n,s[i].stud_name);
strcpy(s[i].stud_name,s[j].stud_name);
strcpy(s[j].stud_name,n);
m1=s[i].m1;
s[i].m1=s[j].m1;
s[j].m1=m1;
m2=s[i].m2;
s[i].m2=s[j].m2;
s[j].m2=m2;
m3=s[i].m3;
s[i].m3=s[j].m3;
s[j].m3=m3;
rno=s[i].rollno;
s[i].rollno=s[j].rollno;
s[j].rollno=rno;
tot=s[i].total;
s[i].total=s[j].total;
s[j].total=tot;
}
}
}
printf("rollno\tname\tmarks1\tmarks2\tmarks3\ttotal\tavg\n");
for(i=0;i<3;i++)
{
printf("%d\t%s\t%d\t%d\t%d\t%d\t%f\n",s[i].rollno,s[i].stud_name,s[i].m1,
s[i].m2,s[i].m3,s[i].total,s[i].avg);
}
getch();
}

/*
enter the roll no :1                                                          
                                                                               
enter the name :peer                                                          
                                                                               
enter the marks 1 :12                                                          
                                                                               
enter the marks 2 :36                                                          
                                                                               
eneter the marks3 :45                                                          
                                                                               
enter the roll no :2                                                          
                                                                               
enter the name :santosh
                                                                               
enter the marks 1 :45                                                          
                                                                               
enter the marks 2 :65                                                          
                                                                               
eneter the marks3 :78

enter the roll no :3

enter the name :amir

enter the marks 1 :98

enter the marks 2 :78

eneter the marks3 :95
rollno  name    marks1  marks2  marks3  total   avg
3       amir    98      78      95      271     90.000000
2       santosh 45      65      78      188     62.000000
1       peer    12      36      45      93      31.000000
*/

No comments: