Grade program average problem

This is a discussion on Grade program average problem within the C Programming forums, part of the General Programming Boards category; I've got everything printed an formatted as per the example in the attachment, but when the program runs, the values ...

  1. #1
    Zaz
    Zaz is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Grade program average problem

    I've got everything printed an formatted as per the example in the attachment, but when the program runs, the values for average are all incorrect. Any ideas/solutions are welcome and appreciated



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char ltrgrade(double x)
    {
         if(x>=90)
                  printf("A");
         else if(x>=80)
                  printf("B");
         else if(x>=70)
                  printf("C");
         else if(x>=60)
                  printf("D");
         else         
                  printf("F");      
     }
    
    
    int main()
    {
        double grade[15][6];
        double total[15];
    	double average[7];
    	double sum=0;
        int a;         
        printf("\t HW1\t HW2\t Quiz1\t Quiz2\tMidterm\tFinal\t Total\t Grade\n"); //: Header://
        
        for(int i=0; i<15; i++)        
          {
             for (int j=0; j<6; j++) 
                {
                 grade[i][j] = 50.0 + rand() % 50; //: generates random grade for each element in the 2d array://
                 total[i] = .05f*grade[i][0]+.05f*grade[i][1]+.1f*grade[i][2]+.1f*grade[i][3]+.3f*grade[i][4]+.4f*grade[i][5];  
                  sum=0;     
                  sum = sum + grade[i][j] ;
                  average[j]= sum/15;   
                 }
            }
           
          
        for(int i=0; i<15; i++)        
          { 
           printf("\n");     
           printf("%d\t", i+1); //: prints the student ID number, (line numbers)://
           for (int j=0; j<6; j++) 
                {
                printf("%6.2f\t", grade[i][j]);
                }
           printf("%6.2f\t  ", total[i]);
           ltrgrade(total[i]);
          }
         printf("\nAverage:"); //: Here for the sake of shiowing where I need it formatted:// 
         
         for(a=0;a<7;a++)
              printf("%6.2f\t", average);
         
         
         printf("\n");       
         system("pause");
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Lots of arithmetic problems!

    You're dividing inside the inner loop - move it to just below the inner loop.
    Also, sum = 0 needs to be moved just before the inner for loop for j, begins. (inside the for i loop, but outside the for j loop.

    To average the HW, quizzes, and tests, you'll need to do the index "flip" trick on the inner for loop. Otherwise, you'll make an average for the rows, instead of the column, as you have already seen.

    From looking at the code, i thought you were trying to get an average score for each student, but later saw you wanted an average for the class, for each test, etc.

    This is completely untested for accuracy, and I have not looked at your assignment at all. The assignment is yours to handle. If you can't describe the programming problem here, I can't help you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void ltrgrade(double x)
    {
         if(x>=90)
                  printf("A");
         else if(x>=80)
                  printf("B");
         else if(x>=70)
                  printf("C");
         else if(x>=60)
                  printf("D");
         else         
                  printf("F");      
     }
    
    
    int main()
    {
      double grade[15][6];
      double total[15];
      double average[7];
      double sum=0.0;
      int i, j, r, c;         
        printf("\n\t HW1\t HW2\t Quiz1\t Quiz2\tMidterm\tFinal\t Total\t Grade\n"); //: Header://
        
        for(i=0; i<15; i++)        
          {
             sum = 0.0;
             for (j=0; j<6; j++) 
             {
                 grade[i][j] = 50.0 + rand() % 50; //: generates random grade for each element in the 2d array://
                 sum = sum + grade[i][j];
             }
             total[i] = .05f*grade[i][0]+.05f*grade[i][1]+.1f*grade[i][2]+.1f*grade[i][3]+.3f*grade[i][4]+.4f*grade[i][5];  
         }
        for(i=0; i<15; i++)        
          { 
           printf("\n");     
           printf("%d\t", i+1); //: prints the student ID number, (line numbers)://
           for (j=0; j<6; j++) 
                {
                printf("%6.2f\t", grade[i][j]);
                }
           printf("%6.2f\t  ", total[i]);
           ltrgrade(total[i]);
          }
         printf("\n\nAverage:"); //: Here for the sake of shiowing where I need it formatted:// 
         //c = column, r = row. note how c & r are "flipped" 
         for(c = 0; c < 6; c++) {
           for(r = 0, sum = 0; r < 15; r++) {
             sum += grade[r][c];  //  flipped here
             //if(c== 6) printf("\t%4.2lf", grade[r][c]); //for debug only
           }
           average[c] = sum/15;
         }
         /*
         This "flipping" of indexes is very common for scoreboards and
         any summation of columns in a table - like here
         */
    
         //average score for all students on 1 HW, Quiz, or Test
         for(i=0;i< 6;i++)
              printf("%6.2f\t", average[i]);
         
         printf("\n\n\t\t\t     press enter when ready");       
         i = getchar();
         return 0;
    }
    That will get you started.

    Remember to have a return "something", for every function that has a return type. If you don't want a return, then make it a void function. main() is the exception - it should always return 0 or "EXIT_SUCCESS" on a normal exit, or some other value, depending on the type of failure.
    Last edited by Adak; 11-20-2009 at 08:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another problem with a program
    By tristangreer in forum C Programming
    Replies: 23
    Last Post: 06-03-2008, 12:52 AM
  2. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  3. Grades program has me lost
    By adrea in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2002, 07:35 PM
  4. Average Rainfall Program Errors
    By JamesAnthony23 in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 10:44 PM
  5. problem with 2d array program.
    By Niloc1 in forum C Programming
    Replies: 1
    Last Post: 04-08-2002, 05:47 PM

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