STRUCTURE AND ITS APPLICATION


This program illustrates the use of structures.


#include"stdio.h"
main()
{
int a,b,n,key;
char p;
struct student
{
char name[50];
int reg;
int m1,m2,m3;
float mavg;
};
struct student s[50],t;
clrscr();
printf("enter the number of records you wish to enter:");
scanf("%d",&n);
for(a=0;a {
printf("enter the name:");
scanf("%s",s[a].name);
printf("enter the register number:");
scanf("%d",&s[a].reg);
printf("enter the marks in three subjects:");
scanf("%d %d %d",&s[a].m1,&s[a].m2,&s[a].m3);
s[a].mavg=(float)((s[a].m1)+(s[a].m2)+(s[a].m3))/3;
printf("--------------------------------------\n");
}
for(a=0;a for(b=0;b if(s[a].mavg {
t=s[a];s[a]=s[b];s[b]=t;
}
printf("\t\t\t THE SORTED RECORD\n\n");
for(a=0;a {
printf("name:%s\n",s[a].name);
printf("register number:%d\n",s[a].reg);
printf("average marks:%g\n",s[a].mavg);
printf("--------------------------------------\n");
}
fflush(stdin);
while(1)
{
printf("do you want to view a record(y/n):");
scanf("%c",&p);
if(p=='n')
{
exit();
}
if(p=='y')
{
printf("enter the register number:");
scanf("%d",&key);
a=0;
while(a {
if(key!=s[a].reg)
a++;
else
break;
}
if(a==n)
{
printf("the student could not be found in the database");
getch();
exit();
}
}
printf("name:%s\n",s[a].name);
printf("marks in three subjects:%d\t %d\t %d\t\n",s[a].m1,s[a].m2,s[a].m3);
printf("average marks:%g\n",s[a].mavg);
printf("-------------------------------------\n");
fflush(stdin);
}
}

Go to the programs page click here!!


Hosted by www.Geocities.ws

1