Diderot Mateo
Diderot Mateo

Reputation: 1

c programming quiz average array within for loop

I'm stuck on a problem. I'm trying to find the average for the QUIZZES in this program assignment. Here's my code. The code in Bold is where my problem is.

 #include <stdlib.h>
 #include <stdio.h>
 #define STUDENTS 4
 #define QUIZZES 5

 main () {

int quizScores [STUDENTS] [QUIZZES] = {
    { 90, 90, 90, 90, 90 },
    { 90, 80, 70, 60, 50 },
    { 90, 89, 88, 87, 86 },
    { 90, 85, 80, 75, 70 }
 };
 int studentTotal = 0, quizTotal, row, col;
 double studentAverage, quizAverage;

 for ( row = 0; row < STUDENTS; row++) {
     studentTotal = 0;
     for ( col = 0; col < QUIZZES; col++) {
         studentTotal += quizScores[row][col];
     }
     studentAverage = (double) studentTotal / QUIZZES;
     printf("Student %i has average %.2lf\n", row, studentAverage);
 }

 **for ( col = 0; col < QUIZZES; col++) {
     quizTotal = 0;
     for ( row = 0; row < STUDENTS; row++) {
         quizTotal += quizScores[col][row];  
     }
     quizAverage = (double) quizTotal / STUDENTS;
     printf("Quiz %i has an average %.2lf\n",col, quizAverage);
     // output the average for this quiz
 }**

system("pause");
}

Upvotes: 0

Views: 646

Answers (1)

Mankarse
Mankarse

Reputation: 40633

You have the indices mixed up in quizTotal += quizScores[col][row];. It should be quizTotal += quizScores[row][col];.

Upvotes: 1

Related Questions