https://www.daniweb.com/community/contribute#

#include <stdio.h>
#include <iostream>


int main(int argc, char*argv[])
{
    int my2d[5][4] ={ 
                       { 36, 14, 41, 24},
                       { 47, 47, 49, 15},
                       { 40, 23, 29, 49},
                       { 17, 23, 7, 7},
                       { 48, 43, 35, 12} };
    int result = 0;                  


// avg

int avg()
float sum=5.4;

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

    printf("The the average number is %.2f\n", sum/5);

}

Need help with calculating the average of each row and the average of the whole 2d array, im new to coding and need help.

Dani AI

Generated

's snippet has a few distinct problems that make the average calculation fail before we even get to the math. Syntax errors (invalid int avg() declaration and stray tokens), mixing C and C++ headers, undefined variables (i, j, a), and loop bounds using <= that go past the array limits all need fixing. is correct: C arrays are zero-based, so a 5-row array is indexed 0..4 and loops should use <, not <=.

Checklist to make the code correct and robust:

  • Decide C or C++ and stick to the appropriate headers (for plain C use <stdio.h> and printf).
  • Use named constants for rows and columns (const int R = 5, C = 4;) to avoid magic numbers.
  • Initialize accumulators to 0.0 and use double (or float) so division yields a fractional average.
  • For each row compute a rowSum, print rowSum / C, and also add rowSum to a total for the overall average.
  • Use for (int i = 0; i < R; ++i) and for (int j = 0; j < C; ++j) to avoid out-of-bounds access.

Example (valid C) that illustrates the pattern:

#include <stdio.h>

int main(void) {
    const int R = 5, C = 4;
    int a[R][C] = {
        {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}
    };
    double total = 0.0;
    for (int i = 0; i < R; ++i) {
        double rowSum = 0.0;
        for (int j = 0; j < C; ++j) rowSum += a[i][j];
        printf("Row %d average: %.2f\n", i, rowSum / C);
        total += rowSum;
    }
    printf("Overall average: %.2f\n", total / (R * C));
    return 0;
}

Notes and quick debugging tips: compile with warnings (gcc -Wall -Wextra -std=c11) to catch off-by-one and type issues; for statically declared arrays the sizes can be derived with sizeof a / sizeof a[0]. Avoid mixing <iostream> with C-style printf unless compiling as C++.

So what's up with line 21?

for(i = 0; i <= 5; i = i + 1) {

I'll call this the row and your for statement goese from 0 to 5 or 6 rows when your array is only 5 rows. Why the equal sign?
Also, why i = i +1 when the usual i++ is the accepted way to write that?
Finally you see line 23 that uses neither i or j but sums up the one array location and also is out of bounds for the array.

You may want to go back to the textbook about how arrays start at zero and if you declare a 5 element array the index is 0 to 4.

That's a lot of initial errors and we have yet to think about averages.

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.