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;
}