0

The average of the first data set should be: (80+70+65+89+90)/5 and the second set (85+80+80+82+87)/5, but for some reason my code is not working.

When I run the code I get 17.0 and 219886384 instead of 78.8 and 82.8.

int main(void)
{    
    int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
    float average;
    int sum;
    int i;
    int j;
    for(i = 0; i < 2; i++)
    {
        sum = 0;
        for(j = 0; j < 5; j++);
        {
            sum += grades[i][j];
        }
        average = sum / 5;
        printf("The average grade for %d is: %f\n", i, average);
    }
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
user24741
  • 15
  • 4

3 Answers3

2

The average is computed using integer arithmetics: sum / 5. Either use sum / 5.0 or define sum as a floating point variable.

There is a silly mistake in this for statement, the extra semicolon after the for clauses is an empty statement, reducing the for to en empty loop followed by a single block, that accesses an entry beyond the end of the array.

    for(j = 0; j < 5; j++);  <--- spurious semicolon!
    {
        sum += grades[i][j];
    }

If you were to put the opening brace on the same line, a style known as Kernighan and Ritchie, such a mistake would become quite unlikely:

    for (j = 0; j < 5; j++) {
        sum += grades[i][j];
    }

Similarly, if you define the loop index inside the for clause, this mistake becomes a syntax error as the loop index would be out of scope in the block:

#include <stdio.h>

int main(void) {    
    int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};

    for (int i = 0; i < 2; i++) {
        float average, sum = 0;
        for (int j = 0; j < 5; j++) {
            sum += grades[i][j];
        }
        average = sum / 5;
        printf("The average grade for %d is: %f\n", i, average);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

As @woz says, part of your problem is that you never run sum += grades[i][j]; in a loop from 0 to 4.

Another problem is that sum and 5 are of the type int, so even after you remove the extra semicolon from the inner loop, you will still have an incorrect output. You should instead do: average = sum / 5.0f;

Thus, your code should be:

#include "stdio.h"
int main(void)
{    
    int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
    float average;
    int sum;
    int i;
    int j;
    for(i = 0; i < 2; i++)
    {
        sum = 0;
        for(j = 0; j < 5; j++) // CHANGE 1: remove semicolon
        {
            sum += grades[i][j];
        }
        average = sum / 5.0f; // CHANGE 2: ensure that the result is a floating point number
        printf("The average grade for %d is: %f\n", i, average);
    }
    return 0;
}

You can run it here.

<script src="//repl.it/embed/JLAN/0.js"></script>

In the future, when you come across such differences between your expected and actual results, add breakpoints to your code to see what it is actually doing before searching for solutions online.

The reason you get the extremely large value is that the integer in memory that immediately follows your array (at the hypothetical 5 position is used to compute the "sum" and "average"). (see relevant SO post)

Jedi
  • 3,088
  • 2
  • 28
  • 47
0

A ';' means the finish of the for loop so your code 'for(j = 0; j < 5; j++);' equals to j=5;

Array Index Out Of Bounds!

Mosliu
  • 16