Hello everyone! I'm trying to solve an exercise.
I have a bidimensional array (N*M) where each element represents how many people live in a square. I have to print which element has more people living in the squares that surround it.

Sorry if you don't understand what I wrote. I will put some pictures, maybe you would understand they better

Help - Bidimensional array-sumamanzanas-png

In the first bidimensional array, the user write how much people live in each square.
In the second bidimensional array, the program "walk" through the bidimensional array and, in this case, it prints that the element which has the higher population in the squares that surrounds the one he lives in is [2][1].

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

int CompleteBidimArray(int N, int M, int matriz[N][M])
{
    int i, j;
    for(i=0; i<N; i++)
    {
        for(j=0; j<M; j++)
        {
            printf("\n[%d][%d]: ", i, j);
            scanf("%d", &matriz[i][j]);
        }
    }
}

int PrintOriginalMatrix(int N, int M, int matriz[N][M])
{
    int i, j;
    for(i=0; i<N; i++)
    {
        for (j=0; j<M; j++)
        {
            printf("%4d", matriz[i][j]);
        }
        printf("\n");
    }
}

int HighLeftSide (int N, int M, int matriz[N][M])
{
    //N=0, M=0
    int sum=0;
    printf("\n\nHIGH LEFT SIDE\n");
    sum = (matriz[1][0]+matriz[1][1]+matriz[0][1]);
    return sum;
}

int HighRightSide (int N, int M, int matriz[N][M])
{
    //N=0, M=M-1
    int sum=0;
    printf("\n\nHIGH RIGHT SIDE\n");
    sum = (matriz[0][M-2]+matriz[1][M-2]+matriz[1][M-1]);
    return sum;
}

int LowerLeftSide (int N, int M, int matriz[N][M])
{
    //N=N-1, M=0
    int sum=0;
    printf("\n\nLOWER LEFT SIDE\n");
    sum = (matriz[N-2][0]+matriz[N-2][1]+matriz[N-1][1]);
    return sum;
}

int LowerRightSide (int N, int M, int matriz[N][M])
{
    //N=N-1, M=M-1
    int sum=0;
    printf("\n\nLOWER RIGHT SIDE\n");
    sum = (matriz[N-1][M-2]+matriz[N-2][M-2]+matriz[N-2][M-1]);
    return sum;
}

int MiddleHigh (int N, int M, int matriz[N][M]) //OK
{
    int i=0, j, sum1=0, sum2=0, sumtotal;
    printf("\n\nMIDDLE HIGH\n");
    for (j=1; j<N; j++)
    {
        sum1 = matriz[0][j-1] + matriz[0][j] + matriz [0][j+1];
        sum2 = matriz[1][j-1] + matriz[1][j] + matriz [1][j+1];
        sumtotal = sum1 + sum2 - matriz[0][j];
        printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
    }
}

int MiddleLow (int N, int M, int matriz[N][M]) //OK
{
    int i=N-1, j, sum1=0, sum2=0, sumtotal;
    printf("\n\nMIDDLE LOW\n");
    for (j=1; j<N; j++)
    {
        sum1 = matriz[i][j-1] + matriz[i][j] + matriz[i][j+1];
        sum2 = matriz[i-1][j-1] + matriz[i-1][j] + matriz[i-1][j+1];
        sumtotal = sum1 + sum2 - matriz[i][j];
        printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumatotal);
    }
}

int RightSide (int N, int M, int matriz[N][M])
{
    int i, j=N, sum1=0, sum2=0, sumtotal;
    printf("\n\nRIGHT SIDE\n");
    for (i=1; i<N-1; i++)
    {
        sum1 = matriz[i-1][j] + matriz[i][j] + matriz[i+1][j];
        sum2 = matriz[i-1][j-1] + matriz[i][j-1] + matriz[i+1][j-1];
        sumtotal = sum1 + sum2 - matriz[i][j];
        printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
    }
}

int LeftSide (int N, int M, int matriz[N][M])
{
    int i, j=0, sum1=0, sum2=0, sumtotal;
    printf("\n\nLEFT SIDE\n");
    for (i=1; i<N-1; i++)
    {
        sum1 = matriz[i-1][j] + matriz[i][j] + matriz[i+1][j];
        sum2 = matriz[i-1][j+1] + matriz[i][j+1] + matriz[i+1][j+1];
        sumtotal = sum1 + sum2 - matriz[i][j];
        printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
    }
}

int Middle (int N, int M, int matriz[N][M])
{
    int i, j, sum1=0, sum2=0, sum3=0, sumtotal;
    printf("\n\nMIDDLE\n");
    for (i=1; i<N-1; i++)
    {
        for (j=1; j<N; j++)
        {
            sum1 = matriz[i-1][j-1] + matriz[i-1][j] + matriz[i-1][j+1];
            sum2 = matriz[i][j-1] + matriz[i][j] + matriz[i][j+1];
            sum3 = matriz[i+1][j-1] + matriz[i+1][j] + matriz[i+1][j+1];
            sumtotal = sum1 + sum2 + sum3 - matriz[i][j];
            printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
        }
    }

}


int main()
{
    int N, M;
    printf("\nNumber of rows: ");
    scanf("%d", &N);
    printf("\nNumber of columns: ");
    scanf("%d", &M);

    int matriz[N][M];
    CompleteBidimArray(N, M, matriz);
    PrintOriginalMatrix(N, M, matriz);


    int suma;
    int MaxSuma = HighLeftSide(N, M, matriz);
    if ((suma = HighRightSide(N, M, matriz))>MaxSuma)
        MaxSuma = suma;
    if ((suma = LowerRightSide(N, M, matriz))>MaxSuma)
        MaxSuma = suma;
    if ((suma = LowerLeftSide(N, M, matriz))>MaxSuma)
        MaxSuma = suma;

    printf("%d", MaxSuma);

    MiddleHigh(N, M, matriz);
    MiddleLow(N, M, matriz);
    RightSide(N, M, matriz);
    LeftSide(N, M, matriz);
    Middle(N, M, matriz);
}
Help - Bidimensional array-sumamanzanas2-png

As you can see, the program prints a lot of thing that are not required. That's just to see if it's working correctly.

- MiddleHigh is OK
- LeftSide is OK


I don't know why, but the program works like if it adds a column in the right side and prints things I didn't write. You can see this in "MiddleLow", "RightSide" and "Middle"


I hope you understand what I wrote!

Thanks,
Juan