• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How to separate the calculation in 2nd for loop

EricEric

New Coder
C:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void main()
{
    FILE *fptr;
    int stud, cour, Sum=0, x, y, number;
    char Grade;
    float AvMark;
    long long studID;

    fptr = fopen("marks.dat", "r");

    if (fptr == NULL)
    {
        printf("File cannot be open\n");
        exit(1);
    }

    printf("SIMPLE GRADING SYSTEM \n");
    printf("\nNo. of students: ");
    fscanf(fptr, "%d", &stud);
    printf("%d", stud);
    printf("\nNo. of courses: ");
    fscanf(fptr, "%d", &cour);
    printf("%d\n", cour);

    for (x=1; x<=stud; x++)
    {
    printf("\nStudent ID: ");
    fscanf(fptr, "%lld", &studID);
    printf("%lld", studID);

        for (y=1; y<=cour; y++)
        {
        printf("\n\n\t Course #%d: ", y);
        fscanf(fptr, "%d", &number);
        printf("%d", number);
        Sum += number;

        if(80<=number && number<=100)
            Grade = 'A';
        else if(70<=number && number<80)
            Grade = 'B';
        else if(60<=number && number<70)
            Grade = 'C';
        else if(50<=number && number<60)
            Grade = 'D';
        else
            Grade = 'F';

        printf("\n\t Grade : %c", Grade);
        }

        AvMark = Sum/cour;

        printf("\n\n\t Total Marks = %d", Sum);
        printf("\n\t Average Mark = %.2f", AvMark);

        if(AvMark>=50)
            printf("\n\n\t PROCEED TO THE NEXT SEMESTER...\n");
        else
            printf("\n\n\t Failure\n");
    }

    fclose(fptr);
}
 

Attachments

  • a.PNG
    a.PNG
    25.6 KB · Views: 2
Last edited by a moderator:

Arthur

New Coder
if I understand the problem right then I think that Sum should be set to equal zero after the average mark calculation
C:
        printf("\n\t Grade : %c", Grade);
        }

    AvMark = Sum/cour;
   
    printf("\n\n\t Total Marks = %d", Sum);
    printf("\n\t Average Mark = %.2f", AvMark);
    Sum = 0; /*Sum was accumulating from one student to the next*/
    if(AvMark>=50)
        printf("\n\n\t PROCEED TO THE NEXT SEMESTER...\n");
    else
        printf("\n\n\t Failure\n");
    }

    fclose(fptr);
 

Top