Friday, 11 August 2017

Array of structure

Q: Define a structure of student with the following fields: rollNo, name and marks. Write a program to read and store the data of at most 30 students in an array. Also display the average marks of
the students.

Solution : 


#include<stdio.h>
#define MAX 3
typedef struct Stu
{
     int roll;
     char name[20];
     int mark;
}student;

void main()
{
     student ch[MAX];
     int i,sum=0;
     for(i=0;i<MAX;i++)
     {
          printf("Enter Roll>> ");
          scanf("%d",&ch[i].roll);
          printf("Enter Name>> ");
          fflush(stdin);
          gets(ch[i].name);
          printf("Enter Marks>> ");
          scanf("%d",&ch[i].mark);
     }
     for(i=0;i<MAX;i++)
     {
          printf("\nRecord %d\nRoll\t%d",i+1,ch[i].roll);
          printf("\nName\t%s",ch[i].name);
          printf("\nMarks\t%d\n\n",ch[i].mark);
     }
     for(i=0;i<MAX;i++)
          sum+=ch[i].mark;
     printf("\nAverage Marks\t%d",sum/MAX);
}

No comments:

Post a Comment