Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help! You are given an n x n array of integers. Let us call this array gr

ID: 3681492 • Letter: P

Question

Please help!

You are given an n x n array of integers. Let us call this array grid. All the elements of grid are either 0 or 1.

Write a function (name the function neighbors) that will take as arguments only two
integer indices i, j , and will return the number of nonzero entries of the array grid that are adjacent to the i - th, j - th element of the array. For example, if the grid array is the 7 x 7 array shown below, then your function should return the following values, when called with the arguments shown below:

neighbors(0,0) should return 0, since there are no non-zero neighbors to element (0,0)
neighbors(0,1) should return 1, since there is 1 non-zero neighbor to element (0,1)
neighbors(3,3) should return 4, since there are 4 non-zero neighbors to element (3,3)
neighbors(3,4) should return 2, …
neighbors(5,3) should return 1, …

Complete the code given below; your task is to fill in the code for the function only.

/* function to find the number of occupied adjacent cells */
int neighbors (int i, int j);

void main ()

{
int i, j, n;

}
     return;

Explanation / Answer

#include <stdio.h>
/* define grid size */
#define SIZE 7
int grid[SIZE][SIZE];
/* function to find the number of occupied adjacent cells */
int neighbors (int i, int j);

int main ()
{
   int i, j, n;
/* initialize the entire grid to be zero */
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
grid[i][j] = 0;
/* introduce a few ones */
grid[1][2] = 1;
grid[2][2] = 1;
grid[1][4] = 1;
grid[2][4] = 1;
grid[3][2] = 1;
grid[3][3] = 1;
grid[3][4] = 1;
grid[5][3] = 1;
grid[6][2] = 1;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++) {
n = neighbors(i,j);
printf ("Number of neighbors to element %d,%d = %d ",i,j,n);
       }
return 0;
}
/* function to compute an element's neighbors */
int neighbors (int i, int j)
{
   int count = 0;
  
   // we need to check in 8 direction
   // (i-1, j), (i-1, j-1), (i-1, j+1), (i+1, j), (i+1, j-1), (i+1, j+1), (i, j-1), (i, j+1)
   if((i-1) >= 0 && (grid[i-1][j]==1)){
       count++;
   }
   if((i-1) >= 0 && (j-1)>=0 && (grid[i-1][j-1]==1)){
       count++;
   }
   if((i-1) >= 0 && (j+1)<SIZE && (grid[i-1][j+1]==1)){
       count++;
   }
   if((i+1)<SIZE && (grid[i+1][j]==1)){
       count++;
   }
   if((i+1) <SIZE && (j-1)>=0 && (grid[i+1][j-1]==1)){
       count++;
   }
   if((i+1) <SIZE && (j+1)<SIZE && (grid[i+1][j+1]==1)){
       count++;
   }
   if((j-1)>=0 && (grid[i][j-1]==1)){
       count++;
   }
   if((j+1)<SIZE && (grid[i][j+1]==1)){
       count++;
   }
   return count;
}

/*

Output:

Number of neighbors to element 0,0 = 0
Number of neighbors to element 0,1 = 1
Number of neighbors to element 0,2 = 1
Number of neighbors to element 0,3 = 2
Number of neighbors to element 0,4 = 1
Number of neighbors to element 0,5 = 1
Number of neighbors to element 0,6 = 0
Number of neighbors to element 1,0 = 0
Number of neighbors to element 1,1 = 2
Number of neighbors to element 1,2 = 1
Number of neighbors to element 1,3 = 4
Number of neighbors to element 1,4 = 1
Number of neighbors to element 1,5 = 2
Number of neighbors to element 1,6 = 0
Number of neighbors to element 2,0 = 0
Number of neighbors to element 2,1 = 3
Number of neighbors to element 2,2 = 3
Number of neighbors to element 2,3 = 7
Number of neighbors to element 2,4 = 3
Number of neighbors to element 2,5 = 3
Number of neighbors to element 2,6 = 0
Number of neighbors to element 3,0 = 0
Number of neighbors to element 3,1 = 2
Number of neighbors to element 3,2 = 2
Number of neighbors to element 3,3 = 4
Number of neighbors to element 3,4 = 2
Number of neighbors to element 3,5 = 2
Number of neighbors to element 3,6 = 0
Number of neighbors to element 4,0 = 0
Number of neighbors to element 4,1 = 1
Number of neighbors to element 4,2 = 3
Number of neighbors to element 4,3 = 4
Number of neighbors to element 4,4 = 3
Number of neighbors to element 4,5 = 1
Number of neighbors to element 4,6 = 0
Number of neighbors to element 5,0 = 0
Number of neighbors to element 5,1 = 1
Number of neighbors to element 5,2 = 2
Number of neighbors to element 5,3 = 1
Number of neighbors to element 5,4 = 1
Number of neighbors to element 5,5 = 0
Number of neighbors to element 5,6 = 0
Number of neighbors to element 6,0 = 0
Number of neighbors to element 6,1 = 1
Number of neighbors to element 6,2 = 1
Number of neighbors to element 6,3 = 2
Number of neighbors to element 6,4 = 1
Number of neighbors to element 6,5 = 0
Number of neighbors to element 6,6 = 0

*/