C Program to store the records of n Students (RollNo , Names , Branch , Year , Marks) and pass the structure as arguement to a function to calculate the average marks.

 


// Program by Akash Tripathi (@proakash256) 
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct student
{
    char RollNo[13];
    char Name[1024];
    char Branch[11];
    int Year;
    int Marks;
};
typedef struct student student;
float average(student *ptr , int *n)
{
    float av = 0;
    for(int i = 0i < *ni = i + 1)
    {
        av = av + ptr[i].Marks;
    }
    av = av / *n;
    return av;
}
int main()
{
    int n;
    printf("Enter the number of Students : ");
    scanf("%d", &n);
    student *st = malloc(n * (sizeof(student)));
    for (int i = 0i < ni = i + 1)
    {
        printf("\nEnter the details of Student %d :\n"i);
        printf("RollNo : ");
        scanf("%s"st[i].RollNo);
        getchar();
        printf("Name : ");
        gets(st[i].Name);
        printf("Branch : ");
        scanf("%s"st[i].Branch);
        getchar();
        printf("Year : ");
        scanf("%d", &st[i].Year);
        getchar();
        printf("Marks : ");
        scanf("%d", &st[i].Marks);
        getchar();
    }
    float av = average(st , &n);
    printf("\nThe Average Marks of all the students is %0.2f\n\n" , av);
    return 0;
}

Comments