Home > front end >  Question for finding the avarage of grades in this situation [C]
Question for finding the avarage of grades in this situation [C]

Time:10-05

I need to find the avarage sum of the grades of each student like if the grades of one are:123 then 1 2 3 = 6/3 =2. And then find the avarage of all the student gardes. Is there any solution to this ?

#include <stdio.h>
#define size 2
struct student {
    char firstName[50];
    int roll;
    float marks;
    int total;
} s[size];

int main() {
    int i;
    printf("Enter information of students:\n");

    // storing information
    for (i = 0; i <size;   i) {
        s[i].roll = i   1;
        printf("\nStudent %d,\n", s[i].roll);
        printf("Enter first name: ");
        scanf("%s", s[i].firstName);
        printf("Enter marks: ");
        scanf("%f", &s[i].marks);
    }

    printf("Displaying Information:\n\n");

    // displaying information
    for (i = 0; i < size;   i) {
        printf("\nStudent %d\n", i   1);
        printf("First name: ");
        puts(s[i].firstName);
        printf("Marks: %.1f", s[i].marks);
        printf("\n");
    }
    return 0;
}

CodePudding user response:

For your consideration, I added a few work variables to receive a varying number of grades for each student, store the grade values as a total and then use the "total" variable as a divisor to derive the student's average. Along with that, there is an average work field used to derive the overall student average. Following is a snippet of code for your analysis.

    #include <stdio.h>
#include <stdlib.h>

#define size 2
struct student {
    char firstName[50];
    int roll;
    float marks;
    int total;
} s[size];

int main() {
    int i;
    float grade, average;   /* Work variables to input grades and derive total average */
    printf("Enter information of students:\n");

    average = 0.0;

    // storing information
    for (i = 0; i <size;   i) {
        s[i].roll = i   1;
        printf("\nStudent %d,\n", s[i].roll);
        printf("Enter first name: ");
        scanf("%s", s[i].firstName);
        s[i].marks = 0.0;
        s[i].total = 0;

        while(1)
        {
            printf("Enter marks or 0.0 to conclude grades for this student: ");
            scanf("%f", &grade);
            if (grade == 0.0)
            {
                break;
            }
            s[i].marks  = grade;
            s[i].total  ;
        }
        average = average   (s[i].marks / s[i].total);
    }

    printf("Displaying Information:\n\n");

    // displaying information
    for (i = 0; i < size;   i) {
        printf("\nStudent %d\n", i   1);
        printf("First name: ");
        puts(s[i].firstName);
        printf("Average marks: %.1f", s[i].marks / s[i].total);
        printf("\n");
    }

    printf("Average grade for all students: %f\n", average / size);

    return 0;
}

Following was some sample input and output.

@Una:~/C_Programs/Console/StudentGrades/bin/Release$ ./StudentGrades 
Enter information of students:

Student 1,
Enter first name: Craig
Enter marks or 0.0 to conclude grades for this student: 88.0
Enter marks or 0.0 to conclude grades for this student: 78.0
Enter marks or 0.0 to conclude grades for this student: 92.0
Enter marks or 0.0 to conclude grades for this student: 0.0

Student 2,
Enter first name: Lily
Enter marks or 0.0 to conclude grades for this student: 94.0
Enter marks or 0.0 to conclude grades for this student: 82.0
Enter marks or 0.0 to conclude grades for this student: 88.0
Enter marks or 0.0 to conclude grades for this student: 0.0
Displaying Information:


Student 1
First name: Craig
Average marks: 86.0

Student 2
First name: Lily
Average marks: 88.0
Average grade for all students: 87.000000

Give that a try and see if that meets the spirit of your project.

  •  Tags:  
  • c
  • Related