Structure related program for C Language
Code for accessing structure and creating a marksheet like project. This code is for creating and accessing structure with an example using C Language.
This is the best and easy code for learning Structure and it’s use. Here each and every functions are used using most simple logic and structure.
After understanding this code you will be able to write any structure related program easily.
For more C Language related code you can Click Here. For more video tutorial on several topic you can visit this link.
#include<stdio.h>
#include<string.h>
typedef struct Student
{
int roll;
char name[50];
int m[3];
int tot;
float avg;
char grade;
}std;
void insertdata(std [],int);
void dispdata(std [],int);
void topper(std [],int);
void topper_each_sub(std [],int);
void arrang_by_name(std [],int);
int main()
{
std s[50];
int m;
printf("Enter the total number of students :");
scanf("%d",&m);
printf("\nEnter the records for %d students",m);
insertdata(s,m);
printf("\nRecords of %d students : ",m);
dispdata(s,m);
printf("\nTopper in the department : ");
topper(s,m);
printf("\nTopper in each subjects : ");
topper_each_sub(s,m);
printf("\nStudent's record in ascending order of name : ");
arrang_by_name(s,m);
dispdata(s,m);
return 0;
}
void insertdata(std p[],int k)
{
int i,j;
for(i=0;i<k;i++)
{
printf("\nInsert records for student %d : ",i+1);
printf("\nEnter the roll number : ");
scanf("%d",&p[i].roll);
getchar();
printf("\nEnter the name : ");
gets(p[i].name);
printf("\nEnter marks for 3 subjects : ");
p[i].tot=0;
for(j=0;j<3;j++)
{
scanf("%d",&p[i].m[j]);
p[i].tot+=p[i].m[j];
}
p[i].avg=p[i].tot/3.0;
if(p[i].avg>=90)
p[i].grade='O';
else if(p[i].avg>=80 && p[i].avg<90)
p[i].grade='E';
else if(p[i].avg>=70 && p[i].avg<80)
p[i].grade='A';
else if(p[i].avg>=60 && p[i].avg<70)
p[i].grade='B';
else if(p[i].avg>=50 && p[i].avg<60)
p[i].grade='C';
else if(p[i].avg>=40 && p[i].avg<50)
p[i].grade='D';
else
p[i].grade='F';
}
}
void dispdata(std p[],int k)
{
int i,j;
printf("\nRoll\tName\t\tMarks1\tMarks2\tMarks3\tTotal\tAvg\tGrade");
printf("\n============================================================================");
for(i=0;i<k;i++)
{
printf("\n%d\t",p[i].roll);
printf("%s\t\t",p[i].name);
for(j=0;j<3;j++)
printf("%d\t",p[i].m[j]);
printf("%d\t%3.2f\t%c",p[i].tot,p[i].avg,p[i].grade);
}
}
void topper(std s[],int m)
{
int i,j,t,max;
max=0;
t=0;
for(i=0;i<m;i++)
{
if(s[i].tot>max)
{
max=s[i].tot;
t=i;
}
}
printf("\nRoll\tName\t\tMarks1\tMarks2\tMarks3\tTotal\tAvg\tGrade");
printf("\n============================================================================");
printf("\n%d\t",s[t].roll);
printf("%s\t\t",s[t].name);
//puts(p[i].name);
for(j=0;j<3;j++)
printf("%d\t",s[t].m[j]);
printf("%d\t%3.2f\t%c",s[t].tot,s[t].avg,s[t].grade);
}
void topper_each_sub(std s[],int k)
{
int i,j,t,max;
for(i=0;i<3;i++)
{
max=0;
t=0;
for(j=0;j<k;j++)
{
if(s[j].m[i]>max)
{
max=s[j].m[i];
t=j;
}
}
printf("\nMaximum marks obtained in Subject %d by :",i+1);
printf("\nRoll\tName\t\tMarks1\tMarks2\tMarks3\tTotal\tAvg\tGrade");
printf("\n============================================================================");
printf("\n%d\t",s[t].roll);
printf("%s\t\t",s[t].name);
//puts(p[i].name);
for(j=0;j<3;j++)
printf("%d\t",s[t].m[j]);
printf("%d\t%3.2f\t%c",s[t].tot,s[t].avg,s[t].grade);
}
}
void arrang_by_name(std s[],int p)
{
std t;
int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<p-i-1;j++)
{
if(strcmp(s[j].name,s[j+1].name)>0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
}