GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Nov-2007, 18:02
CaitlynCraft CaitlynCraft is offline
New Member
 
Join Date: Oct 2007
Posts: 5
CaitlynCraft is on a distinguished road

Gradebook Program - Small Problem, Need Assistance


Hi everyone, I am going to post a gradebook program I wrote recently, 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.

CPP / C++ / C 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  
Old 13-Nov-2007, 20:43
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 958
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Gradebook Program - Small Problem, Need Assistance


Merely change this line in the colAverage() function:
CPP / C++ / C Code:
            colAvrg[col] += table [col][row];
To this:
CPP / C++ / C Code:
            colAvrg[col] += table [row][col];

Output
Code:
Exm1 Exm2 Exm3 Stu1 40 50 60 | 50.00 Stu2 55 65 75 | 65.00 Stu3 85 95 100 | 93.33 ------------------------------------------------- 60.00 70.00 78.33

Also, where you have declared 'row' and 'col' as integers, there is no need to initialize them with 0.0 -- just 0 is ok.
There were warnings for all those:
Code:
... : warning C4244: '=' : conversion from 'const double' to 'int', possible loss of data
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 13-Nov-2007, 20:47
CaitlynCraft CaitlynCraft is offline
New Member
 
Join Date: Oct 2007
Posts: 5
CaitlynCraft is on a distinguished road

Re: Gradebook Program - Small Problem, Need Assistance


I can't thank you enough! That worked! Smiles!
  #4  
Old 13-Nov-2007, 23:01
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 469
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: Gradebook Program - Small Problem, Need Assistance


Also be aware that down a couple of lines below what TurboPT referred to you have:
CPP / C++ / C Code:
         colAvrg [col] /= MAX_ROWS;
...which is not noticed in output because table[MAX_ROWS][MAX_COLS] is 3x3.
But if you were to change that size in the future????

Do yourself a favor in the future and place some strategic printf()'s to check your values like:
CPP / C++ / C Code:
    /* in main() */
    colAverage (table, colAve);
    for(i = 0; i < MAX_COLS; i++)
    {
       printf("colAve[%d]= %f  ", i, colAve[i] );
    }
    /* and this down in colAverage()... */
    for (col = 0.0; col < MAX_COLS; col++)
    {
        printf("colAvrg");
        for (row = 0.0; row < MAX_ROWS; row++)
        {
            colAvrg[col] += table [row][col];
            printf("[%d]= %3.3f,  ",col, colAvrg[col]);
        }
        colAvrg [col] /= MAX_COLS;
        printf(" colAvrg[%d]= %3.3f \n", col, colAvrg [col] );
    }
They will show EXACTLY what is going on.
It takes a little time but reduces facial squinch lines in the long run...
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Problem compiling MFC program shvalb MS Visual C++ / MFC Forum 1 21-Sep-2006 20:14
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 19:37
Fresh Small C++ Program :P Sabin044 C++ Forum 40 02-Feb-2006 18:57
Small help with this program please NigelZen C Programming Language 3 28-Aug-2005 23:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.