'How can I correctly display all the elements of 2D array that have even neighbors?

I wrote a program that finds and displays all the elements in the 2D array, which have all neighbors(left, top, right, bottom) - even. I tried to put in an if, but the result is wrong. I think because it checks for non-existent elements, if the current element is on the edge. How can I fix it? Thank you in advance.

#include <stdio.h>
#define n 5
#define m 9

int main()
{
    int i, j;

    int array[n][m] = {
            {-1, 4, 12, 2, 10, -1, 8, 7, 6},
            {18, 3, 2, 0, 0, 10, 10, 0, 0},
            {9, 18, 3, 12, 5, 18, 4, 7, 0},
            {6, 3, 11, 5, 11, 1, 1, 1, 22},
            {8, 7, 46, 20, 1, 2, 3, 4, 5},
    };

    // int m,n;
    // printf("Rows and Columns: \n");
    // scanf("%d %d", &n, &m);

    // int array[n][m];

    // printf("Enter elements: \n");
    // for (i = 0; i < n; i++)
    //   {
    //           for (j = 0; j < m; j++)
    //           {
    //                scanf("%d", &array[i][j]);
    //            }
    // }
    printf("Matrix: \n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            printf(" %3d", array[i][j]);
        }
        printf("\n");
    }

    printf("Elements with all neighbors even are: \n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if ((array[i-1][j] % 2) == 0 && (array[i+1][j] % 2) == 0 && (array[i][j-1] % 2) == 0 && (array[i][j+1] % 2) == 0)
            {
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);}
        }
    }
}


Solution 1:[1]

first, you have to check for particular element of array that neighbor exists or not by applying condition as shown in below code and then you have to check for the even number.

#include <stdio.h>
#define n 5
#define m 9

int main()
{
    int i, j;

    int array[n][m] = {
            {-1, 4, 12, 2, 10, -1, 8, 7, 6},
            {18, 3, 2, 0, 0, 10, 10, 0, 0},
            {9, 18, 3, 12, 5, 18, 4, 7, 0},
            {6, 3, 11, 5, 11, 1, 1, 1, 22},
            {8, 7, 46, 20, 1, 2, 3, 4, 5},
    };

    // int m,n;
    // printf("Rows and Columns: \n");
    // scanf("%d %d", &n, &m);

    // int array[n][m];

    // printf("Enter elements: \n");
    // for (i = 0; i < n; i++)
    //   {
    //           for (j = 0; j < m; j++)
    //           {
    //                scanf("%d", &array[i][j]);
    //            }
    // }
    printf("Matrix: \n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            printf(" %3d", array[i][j]);
        }
        printf("\n");
    }

    printf("Elements with all neighbors even are: \n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if( n==1 && m==1 ){
                printf("No neighbour exist");
            }

            else if( i==0 && j ==0 ){
                if ((array[i+1][j] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
                
            }
            else if( i==0 && j == (m-1) ){
                if ((array[i+1][j] % 2) == 0 && (array[i][j-1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if( i== (n-1) && j==0 ){
                if ((array[i-1][j] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if( i== (n-1) && j==8 ){
                if ((array[i-1][j] % 2) == 0 && (array[i][j-1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if( i == 0 ){
                if ((array[i+1][j] % 2) == 0 && (array[i][j-1] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if ( j == 0 ){
                if ((array[i+1][j] % 2) == 0 && (array[i-1][j] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if ( j == ( m-1 ) ){
                if ((array[i+1][j] % 2) == 0 && (array[i-1][j] % 2) == 0 && (array[i][j-1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else if ( i == (n-1) ){
                if ((array[i-1][j] % 2) == 0 && (array[i][j-1] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }

            else 
            {
                if ((array[i-1][j] % 2) == 0 && (array[i+1][j] % 2) == 0 && (array[i][j-1] % 2) == 0 && (array[i][j+1] % 2) == 0)
                printf("a[%d][%d]= %2d\n",i,j, array[i][j]);
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1