gpa program not working

This is a discussion on gpa program not working within the C Programming forums, part of the General Programming Boards category; I am trying to complete an example in my book and I dont want to go any further until I ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Question gpa program not working

    I am trying to complete an example in my book and I dont want to go any further until I complete it. It wants me write a function that prints the average for each student in a class and prints the class average. Let an A grade have Value 4, a B grade have value 3, and so forth. I have been pulling my hair out and I got t to print out the class average but I am lost as to how to get the student average. Here is the code:



    in the .h file
    Code:
    #include <stdio.h>
    
    
    #define CLASS_SIZE  100
    
    struct student{
    	char	*last_name;
    	int		student_id;
    	char	grade;
    };
    
    in the .c file
    
    
    
    #include "class_info.h"
    #include <stdio.h>
    void average(struct student     class[], int length);
    int main(void)
    {
       struct student           class[class_size];
    
    
        class[0].grade = 'A';
        class[0].last_name = "Walker";
        class[0].student_id = 590017;
    
        class[1].grade = 'B';
        class[1].last_name = "Smith";
        class[1].student_id = 590118;
    
        class[2].grade = 'C';
        class[2].last_name = "jones";
        class[2].student_id = 590219;
    
        class[3].grade = 'A';
        class[3].last_name = "Bubba";
        class[3].student_id = 590320;
    
        class[4].grade = 'B';
        class[4].last_name = "johns";
        class[4].student_id = 590421;
    
        class[5].grade = 'C';
        class[5].last_name = "Walker";
        class[5].student_id = 590017;
    
        class[6].grade = 'D';
        class[6].last_name = "Smith";
        class[6].student_id = 590118;
    
        class[7].grade = 'B';
        class[7].last_name = "jones";
        class[7].student_id = 590219;
    
        class[8].grade = 'A';
        class[8].last_name = "Bubba";
        class[8].student_id = 590320;
    
        class[9].grade = 'C';
        class[9].last_name = "johns";
        class[9].student_id = 590421;
    
    	average( class, 10 );
    
        return 0;
    }
    
    
    void average(struct student     class[], int length)
    {
        double sum=0.0,avg;
        int i;
    //    int length = sizeof(class)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' ) 
            sum+=4;
            if( class[i].grade == 'B' ) 
            sum+=3;
            if( class[i].grade == 'C' ) 
            sum+=2;
            if( class[i].grade == 'D' ) 
            sum+=1;
            if( class[i].grade == 'F' ) 
            sum+=0;
    
        }
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg)
    }
    can someone lead me out of the darkness. It will print the class average now all i have to do is get it to print out the student and his average with a=4, b=3, c=2, d=1

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    void average(struct student     class[], int length)
    {
        double sum=0.0,avg;
        int i;
    //    int length = sizeof(class)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' ) 
            sum+=4;
            if( class[i].grade == 'B' ) 
            sum+=3;
            if( class[i].grade == 'C' ) 
            sum+=2;
            if( class[i].grade == 'D' ) 
            sum+=1;
            if( class[i].grade == 'F' ) 
            sum+=0;
    
        }
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg)
    }
    You need to simply loop through the array of structs, similar to what you did for the class grade average, above.

    But no "If (class[i].grade ==...) this time. Now it's just:

    Code:
    for (i = 0; i < length; i++)  
       printf(" %s, %c", class[i].name, class[i].grade);
    If you need to add more print statements, remember to add curly braces before and after the lines after the for (...), code line.

    Also, note that CLASS_SIZE is not the same as class_size. Just a typo, maybe?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27

    Question

    I cleaned it up and put that snippet of code in . It prints out the name and Grade like this
    Walker A
    Walker B
    I need to add the grades of each student and divide them by the grades and show the average.
    Can some one start me off?

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Here are a few other things

    Code:
    for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' ) 
            sum+=4;
            if( class[i].grade == 'B' ) 
            sum+=3;
            if( class[i].grade == 'C' ) 
            sum+=2;
            if( class[i].grade == 'D' ) 
            sum+=1;
            if( class[i].grade == 'F' ) 
            sum+=0;
    
        }
    If you are going to use the K&R style, then the for loop should be constructed as

    Code:
    for( i=0;i<length;++i ) {
    
    }
    As opposed to emulating the Pascal loops.


    Code:
        printf("The average for the class is %f.\n", avg)
    printf() is missing the semicolon.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matthughes View Post
    I cleaned it up and put that snippet of code in . It prints out the name and Grade like this
    Walker A
    Walker B
    I need to add the grades of each student and divide them by the grades and show the average.
    Can some one start me off?
    First thing I'd do is get rid of the duplicate student records, Bubba, Walker, etc. You might have two students with the same last (even first), names, but they shouldn't have the same student ID number! That will clear up some unneeded confusion.

    To do what you want just add the curly braces I mentioned in the previous post around all the statements following the for () code line.

    Then use a sum or total variable, to keep a sum of all the grades, using the A == 4,
    B == 3, etc., method.
    Code:
    if (grade == 'A') sum += 4;
    if (grade == 'B') sum += 3;
    For all the grades. When you have finished the for loop, your sum will be ready to be divided by the number of students in the class. If you want something more accurate than integer division which cuts down to the next lowest integer, then make sum and student number, either floats or doubles, rather than integers. (You could also cast them, but I'm thinking that's something you haven't studied, yet.)

    That will give you your average.

    You'll need to add the print statement to show the new data (average, whatever), after the for loop.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    27
    I am so novice beginner that this is all I come up with
    Code:
    	for (i = 0; i < length; i++)
    	{
    		if(class[i].grade == 'A' && class[i].student_id == 1)
    		total += 4, gradecnt +=1;
    		if(class[i].grade == 'B' && class[i].student_id == 1)
    		total += 3, gradecnt +=1;
    		if(class[i].grade == 'C' && class[i].student_id == 1)
    		total += 2, gradecnt +=1;
    		if(class[i].grade == 'D' && class[i].student_id == 1)
    		total += 1, gradecnt +=1;
    		studavg = (total/gradecnt);
    		next +1;
    	}
    		printf("The average for the Walker is %f.\n", studavg);
    when i do this with the studavg it messes up all the other students can someone point out something better? than doing the if statement 16 times to test the grade and id ?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My previous post was off the mark because I thought each struct in the array, represented a different student, not repetitions of the same student, with another test grade, entry.

    Imo, it's awkward to deal with the data, when it's organized this way.

    This is a one file version. I changed the struct's array to class1, and changed the Average function you wrote to ClassAverage, etc.

    Enjoy!

    Code:
    #include <stdio.h>
    
    /* #define class1_SIZE  100 */
    
    struct student {
    	char	*last_name;
    	unsigned long int	student_id;
    	char	grade;
    };
    
    void ClassAverage(struct student*, int length);
    void StudentAverage(struct student*, int length);
    
    int main(void)
    {
       int i;
       struct student class1[10];
    
       class1[0].grade = 'A';
       class1[0].last_name = "Walker";
       class1[0].student_id = 590017;
    
       class1[1].grade = 'B';
       class1[1].last_name = "Smith";
       class1[1].student_id = 590118;
    
       class1[2].grade = 'C';
       class1[2].last_name = "Jones";
       class1[2].student_id = 590219;
    
       class1[3].grade = 'A';
       class1[3].last_name = "Bubba";
       class1[3].student_id = 590320;
    
       class1[4].grade = 'B';
       class1[4].last_name = "Johns";
       class1[4].student_id = 590421;
    
       class1[5].grade = 'C';
       class1[5].last_name = "Walker";
       class1[5].student_id = 590017;
    
       class1[6].grade = 'D';
       class1[6].last_name = "Smith";
       class1[6].student_id = 590118;
    
       class1[7].grade = 'B';
       class1[7].last_name = "Jones";
       class1[7].student_id = 590219;
    
       class1[8].grade = 'A';
       class1[8].last_name = "Bubba";
       class1[8].student_id = 590320;
    
       class1[9].grade = 'C';
       class1[9].last_name = "Johns";
       class1[9].student_id = 590421;
    
       for (i = 0; i < 10; i++)
         printf("\n&#37;s %ld %c", class1[i].last_name, class1[i].student_id, class1[i].grade);
    
    
       ClassAverage( class1, 10);
       StudentAverage( class1, 10);
       printf("\n\n\t\t      Program Complete - Enter to Continue ");
       getchar();
       return 0;
    }
    
    void ClassAverage(struct student *class1, int length)
    {
        double sum=0.0,avg;
        int i;
    //    int length = sizeof(class1)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class1[i].grade == 'A' ) 
            sum+=4;
            if( class1[i].grade == 'B' ) 
            sum+=3;
            if( class1[i].grade == 'C' ) 
            sum+=2;
            if( class1[i].grade == 'D' ) 
            sum+=1;
            /* anything that just adds zero may be eliminated from your code */
            if( class1[i].grade == 'F' ) 
            sum+=0;
    
        }
        avg = (sum/length);
        printf("\n\nThe average for the class is %.2f.\n", avg);
    }
    
    void StudentAverage(struct student *class1, int length) {
       int i, j, recnum;
       unsigned long int target_id;
       double average, sum;
    
       for (i = 0, target_id = 0; i < length; i++)   {   
          if (class1[i].student_id > target_id) {   /* we have a new student */
             target_id = class1[i].student_id;
             printf("\n Student Found - Name: %8s  ID #: %ld  Average Grade: ", \
             class1[i].last_name, class1[i].student_id);        
               
             recnum = sum = 0;
             for (j = i; j < length; j++)  {   /* get all the matching records */
                if (class1[j].student_id == target_id)  {  /* got one */      
          
                   /* this is cute */
                   if (class1[j].grade < 'E')
                      sum += ((class1[j].grade - 'E') * - 1);    
                
                   recnum++;
                   average = sum / recnum;  
                }
             }   
             printf("%.2f ", average);      
          }  
       }
    }
    Last edited by Adak; 05-20-2007 at 07:18 AM.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    27
    Wow thanks for explaining it like that the way I had it I couldnt sort the grades between students and the class average and the first one that pulled up would distort the averages of the other students

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Glad you liked it, Matt. I'm very pleased with the program, now. (and I was scowling WTF when I saw how the data REALLY was arranged), for sure. I am not used to that!

    That algorithm for StudentAverages() is one I'm going to have to keep in my "dark secret algo's" notebook. It handles that messy data, in good order, without needing sorting, or indexing.

    The performance would not be good with huge amounts of data, but when was the last time you saw a class with tens of thousands of student grades?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-08-2008, 12:38 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 11:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21