I'm stuck on my MAXROW is suppose to be 40 but everytime I input it I get a weird output. I'm pretty sure my counter is correct. Can someone please take a lookk and direct somehow.

#include <stdio.h>
#include <stdlib.h>

#define MAXROW 15
#define MAXCOL 6
#define ValuesPerLine 10
#define FLUSH fflush(stdin)


//Global declarations
void getData (int studentScores [][MAXCOL], int *rowcount, int *colcount);
void rowAverage (int studentScores [][MAXCOL], int rowcount, float rowAvrg[]);
void colAverage (int studentScores [][MAXCOL], int rowcount, float colAvrg[]);
void printTable (int studentScores [][MAXCOL], int rowcount, float rowAvrg [], float colAvrg[]);

int main (void)
{
   //Local Declarations 
  FILE* studentGrades;
  FILE* studentFinal;
  int studentScores[MAXROW][MAXCOL];
  int rowcount, colcount;
  float rowAve [MAXROW] = {0.0};
  float colAve [MAXCOL] = {0.0};
  int mn [MAXCOL] = {0};
  int mx [MAXCOL] = {0};
  
  
  //Statements
  getData (studentScores, &rowcount, &colcount);
  rowAverage (studentScores, rowcount, rowAve);
  colAverage (studentScores, rowcount, colAve);



  
  printf("\n");
  //printTable (studentScores, rowAve, colAve, lowGrade);
  printTable (studentScores, rowcount, rowAve, colAve);


          

 

printf("\nend of program\n"); //closing statement
system("PAUSE"); 
return 0;
}//end of program


/* ===============================Get Data==================================
In this function we will get the data from the file and have it stored into the system
inorder to have the array of the file. The array will print out onto the screen.
The function will open the data file and read from file and when done will close 
the file.
*/


void getData (int studentScores [][MAXCOL], int *rowcount, int *colcount)
{
  FILE* studentGrades;
  int row, col;

   studentGrades = fopen("studentGrades.txt", "r");  //reads file
   if ((studentGrades = fopen("studentGrades.txt", "r")) == NULL)  //checks if file exists
   {
   printf("\aERROR OPENING studentGrades.txt\n");
   exit (100);
   }
   

   for(row=0; row < MAXROW; ++row)
   {
     for(col=0; col < MAXCOL; ++col)
     { 
     *rowcount = row;
     *colcount = col;
     fscanf(studentGrades, "%d", (*(studentScores+row)+col));       //Pointer notation.
       //scanf("%d", (*(*(array+r)+c)));  //This line shows an error!
       
     }
   }
  // *rowcount = row;
  // *colcount = col;


          
            if (fclose(studentGrades)== EOF)  //closing file
            {
            printf("\aError closing studentGrades.txt\n");
            exit (102);
            }// if close
            
   return;
}
     
/* ===============================Row/Student Average==================================
This function will calculate the average of the students quiz 1, 2, 3 will be 50% of 
the student grade while quiz 4 & 5 will be the other 50% of the student calculation 
to get the students final grade
*/

void rowAverage (int studentScores [][MAXCOL], int rowcount, float rowAvrg[])
{
     //Statements
     for (int row = 0; row < rowcount; row++)
     {
         for (int col = 1; col < MAXCOL; col++)
         //calculating the student average
         rowAvrg [row] = (((studentScores [row][1] + studentScores[row][2] + studentScores[row][3]) /3.0) *0.5)+
                       (((studentScores [row][4] + studentScores [row][5]) /2.0) * 0.5);
   }
   return;
}

/* ===============================Colum/Quiz Average==================================
In this function we get the average of the quizzes. Example the class average of quiz 1,
quiz 2, quiz 3, quiz 4, and quiz 5. and will print it out as a one dimensional array
*/

void colAverage (int studentScores [][MAXCOL], int rowcount, float colAvrg[])
{
     //Statements
     for (int col = 1; col < MAXCOL; col++)
     {
        for (int row = 0; row < rowcount; row++)
        //calculating the class average of quizzes
        colAvrg[col] += (*(*(studentScores+row)+col));
         colAvrg [col] /= rowcount;
   }
   return;
}



         
/*==============================Printing The table============================
This function will call the functions and print them out accordingly.
*/

void printTable (int studentScores [][MAXCOL], int rowcount, float rowAvrg [], float colAvrg[])
{
        for (int row = 0; row < rowcount; row++)
        {
         for (int col = 0; col < MAXCOL; col++)
         printf("  %5d", (*(*(studentScores+row)+col)) );   //prints out what was on the data file.
         printf("   |%6.2f\n", rowAvrg [row]); // prints out student average
         }
         
         
         printf("\n");
         printf("Average");
         printf("   ");
         for (int col = 1; col < MAXCOL; col++) 
         printf("%7.2f", colAvrg[col]);  // prints out quiz averages
         
            
     
         printf("\n\n");

         
         
         return;
}

Recommended Answers

All 4 Replies

Some confusing bits in your code.
Sometimes you start your column loop with 1 and sometimes 0.
If you used CODE=C you would get line numbers for reference!

Remove
*rowcount = row;
*colcount = col;
from within the double loop. You pass in an array of known fixed size MAXCOL but then you're setting the colcount?

Set rowcount outside the loop! And since you also know its size since the loop is hardcoded MAXROW MAXCOL, the pointer to counts are redundant!

Arrange our code so its more readable. You have the quiz 123 = 50% vs 50% outside the function but put a notation near the equation. And orient your code to make it more apparent and try to align them somewhat keeping the sum of 3 and the sum of 2 visually separate! And you have an error in your score tally. You're using a double loop, row x col but you've hardcoded your col references.

for (int row = 0; row < rowcount; row++)
     {
         //calculating the student average
         rowAvrg [row] = (((studentScores [row][1] 
                      + studentScores[row][2] 
                      + studentScores[row][3]) /3.0) *0.5)+
                       (((studentScores [row][4] 
                            + studentScores [row][5]) /2.0) * 0.5);
   }

If you aren't going to use braces on concatenated lines (which is a mistake) then atleast put a blank line between it and the next line!
Also notice the blank line I added in the following code. Also did you mean col = 1 ??? What happened to 0? I mentioned this earlier!

for (int col = 1; col < MAXCOL; col++)
     {
        for (int row = 0; row < rowcount; row++)
                     //calculating the class average of quizzes
                      colAvrg[col] += (*(*(studentScores+row)+col));

         colAvrg [col] /= rowcount;
   }

Please cleanup and repost!

I understand what you mean by the redundancy but that's what was stated the file reading has StudId, Quiz, Quiz, Quiz, Quiz, Quiz,
am reading in 15 rows but the max will be 40. Which is what I need the counter.

Ok thanks I understand on the quiz 123 = 50% vs 50%.

ohh the reason I'm using on the col = 1 is because the file read in starts with the StudId.

is this what you mean by taking it out of the loop I still get the same output.

for(row=0; row < MAXROW; ++row)
   {
     for(col=0; col < MAXCOL; ++col)
     { 
     fscanf(studentGrades, "%d", (*(studentScores+row)+col));       //Pointer notation.
    
     }
   }
  *rowcount = row;
  *colcount = col;

I understand what you mean by the redundancy but that's what was stated the file reading has StudId, Quiz, Quiz, Quiz, Quiz, Quiz,
am reading in 15 rows but the max will be 40. Which is what I need the counter.

Then you should use a while loop instead of a for loop. The for loop is meant to loop a specific number of times, and the while loop is used to loop until a specific condition is met -- like reading the last line in a file.

You may want to post again, use the code tages with the C assignment CODE=C so we get line numbers. Change your tabs to spaces, about 3 characters each tab should do it. It'll make your code easier to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.