Thread: Gradebook Program - Small Problem, Need Assistance

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Gradebook Program - Small Problem, Need Assistance

    Hi everyone, I am going to post a gradebook program I wrote recently (in C programming, using DevC), but am having some trouble with. I need to have averages in both the Columns and Rows, but can't seem to get the Columns to work, right now it just shows the same as the Rows...If anyone can see my error, which I am sure is there, please let me know, and thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_ROWS 3
    #define MAX_COLS 3
    
    
    void getData      (int   table[][MAX_COLS]); 
    void rowAverage   (int   table[][MAX_COLS],
                       float rowAvrg []);
    void colAverage   (int   table[][MAX_COLS],
                       float colAvrg []);
    void  printTables (int   table[][MAX_COLS], 
                       float rowAvrg[], 
                       float colAvrg[],
                       FILE *fout);
    
    int main (void)
    {
    
        int   table  [MAX_ROWS][MAX_COLS];
    
       FILE *fout;
       
       fout = fopen ("studentGrades.dat", "w");
    
        float rowAve [MAX_ROWS] = {0.0};
        float colAve [MAX_COLS] = {0.0};
    
    
        getData (table);
        rowAverage (table, rowAve);
        colAverage (table, colAve);
    
        printf("\n");
        printTables (table, rowAve, colAve, fout);
        printf("Open up \'studentGrades.dat\' to see the grades...\n");
        system ( "PAUSE" );
        return 0;
    }
    void getData (int table[][MAX_COLS]) 
    {
         int row;
         int col;
    
        
        for (row = 0.0; row < MAX_ROWS; row++)
           for (col = 0.0; col < MAX_COLS; col++)
               {
                printf("\nEnter a grade for student %d and grade %d: ", row+1, col+1);
                scanf("%d", &table[row][col]);
               }
        return;
    }
    
    void rowAverage (int    table[][MAX_COLS],
                     float  rowAvrg [])
    {
             int row;
             int col;
              
        for (row = 0.0; row < MAX_ROWS; row++)
            {
             for (col = 0.0; col < MAX_COLS; col++)
                 rowAvrg[row] += table [row][col];
             rowAvrg [row] /=  MAX_COLS;
            }
        return;
    }
    
    void colAverage (int   table[][MAX_COLS],
                       float colAvrg [])
    {
        int row;
        int col;
        
        for (col = 0.0; col < MAX_COLS; col++)
        {
            for (row = 0.0; row < MAX_ROWS; row++)
            {
                colAvrg[col] += table [col][row];
            }
            colAvrg [col] /= MAX_ROWS;
        }
        return;                      
    }
    void printTables (int    table[][MAX_COLS],
                      float  rowAvrg[], 
                      float  colAvrg[],
                      FILE *fout)
    {
              int row;
              int col;
    
        
        for (col = 1; col <= MAX_COLS; col = col + 1)
        {
            fprintf(fout, "  Exm%d"  , col);   
        }
        
            fprintf(fout, "\n"); 
        
        
        for (row = 0.0; row < MAX_ROWS; row++)
           {
            fprintf (fout, "Stu%d ", row+1);
            for (col = 0.0; col < MAX_COLS; col++)
            fprintf (fout,"%4d", table[row][col]);
            fprintf (fout,"   | %4.2f\n", rowAvrg [row]);
           }
    
    fprintf (fout,"-------------------------------------------------\n");
    fprintf (fout,"     ");
    
            for (col = 0.0; col < MAX_COLS; col++)
            fprintf (fout,"%6.2f", colAvrg [col]);
            
        return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    Silly me...I needed to change one line to this:

    Code:
    colAvrg[col] += table [row][col];
    Instead of what I had!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  2. Small Problem that could be a big problem
    By sytaylor in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2004, 09:49 AM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM