Thread: urgent please help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    urgent please help

    this is a c program. i have to calculate gpa.Grade point average is calculated by multiplying the number of credits earned for a course by the point equivalent of the letter grade for that course. That product is computed for each course and summed together as the total number of points earned. That total is then divided by the number of total credits attempted to give you the GPA.
    Below is a example of how GPA is calculated.
    Chemistry B 4: 3 points for grade x 4 credits = 12
    Math A 3: 4 points for grade x 3 credits = 12
    Gym C 1: 2 points for grade x 1 credit = 2
    =====================================
    This is a total of 26points. The 26 points is then divided by total number of credits to get GPA.
    GPA = 26/8 = 3.25

    i am getting wrong answer for totalpoints and gpa.
    totalpoint should be =49
    gpa should be=3.2

    this is the scores i am using

    Ariel Lewis
    Biology A 4.0
    Statistics C 4.0
    History A 3.0
    Tennis A 1.0
    English B 3.0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //    Local Declarations 
        FILE* spIn;
        float credits;            // number of credits
        float gpa;                // calculate GPA
        char name;                 // Student name
        char grade;                 // letter grade earned
        char course[25];         // subject
        char string[60];
    
        float count=0;
        float totalPoints = 0;
        float totalCredits=0;
        
    void main()
    {
            
    //    Statements 
        spIn = fopen("grades.txt", "r");
        if (!spIn)
           {
            printf("Could not open file\a\n");
    
            exit  (101);
           } // if open fail 
        fgets(string, 60, spIn ); // read in name
        printf("%s\n", string );
        printf("         Ariel Lewis Grade Point Average Calculator\n\n");
        printf("      Course Name     Grade Earned    Number of Credits Earned\n\n");
        while(fscanf(spIn, "%s %c %f \n",course, &grade, &credits )!=EOF) // read in name, course name, grade, credits.
            printf("%15s      %3c     %12.1f \n\n",course, grade, credits  );
        fclose(spIn);   
        /* close the file after reading in data */
    
        /* calculate gradepoint average of student */
        
        
        for(int i = 0; i <= 4; i++)
        {
    
            if (grade == 'A'){                            // check if grade is= to A
                count ++;                            // then point earned = 4    
                totalPoints += 4.0;
            }
            else if (grade == 'B') {                    // check if grade is= to B
                count++;                            // then point earned = 3    
                totalPoints += 3.0;
            }
            else if (grade == 'C'){                    // check if grade is= to C
                count++;                            // then point earned = 2    
                totalPoints += 2.0;
            }
            else if (grade == 'D'){                    // check if grade is= to D
                count++;                            // then point earned = 1    
                totalPoints += 1.0;
            }
            else if (grade == 'F'){                    // check if grade is= to F
                count++;                            // then point earned = 0    
                totalPoints += 0.0;
            }
                         
        }
        printf("\ncredits:  %.1f \n\n\n",credits);
        totalCredits=credits*totalPoints;
        gpa=totalCredits/totalPoints;
        printf("----------------------------------------------------------------------\n");
        printf("\nThe Total Credits earned by this student are:  %.1f \n",totalCredits);
        printf("\nThe GPA for this Student is:  %.1f \n\n\n",gpa);
        printf("----------------------------------------------------------------------\n");
        system("pause");
        return ;
    }    // main

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    First of all, your while loop with the scanf statement isn't storing ALL of your course grade information. It goes until the end of the file and keeps overwriting the value of course, grade, and credits. The last course is English, you got a B in it so in your for loop, you are calculating the totalCredits of the same class 5 times. 3+3+3+3+3 = 15, credits = 3 and 15x3 = 45. Then, gpa = 45/15 which is 3.0. You need to make it so that your program does not just run the for loop using the same value for grade every time.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're overwriting your data with each iteration of this loop:

    Code:
        while(fscanf(spIn, "%s %c %f \n",course, &grade, &credits )!=EOF) // read in name, course name, grade, credits.
            // ...
    Consider using arrays and an incrementing counter [perhaps with a structure], to handle this data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent help on ls- ld
    By vaibhavs17 in forum Tech Board
    Replies: 10
    Last Post: 05-18-2009, 01:50 PM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  3. need some (urgent) help
    By the Wookie in forum Tech Board
    Replies: 3
    Last Post: 06-27-2003, 07:51 AM
  4. very urgent
    By chandramouli_cm in forum C Programming
    Replies: 2
    Last Post: 09-17-2002, 05:07 AM
  5. Urgent!!!!!!please Help Me!
    By duffy in forum C Programming
    Replies: 2
    Last Post: 09-06-2002, 02:13 AM