Question: You are given an n ? n array of integers. Let us call this array grid . All the elements of grid are either 0

You are given an n ? 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?7 array shown below, then your function should return the following values, when called with the arguments shown below:

You are given an n ? n array of integers. Let us

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. Turn in a hardcopy of the code together with a snapshot of its output.

#include  
/* 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);

void main ()

{ int i, j, n;

 /* initialize the entire grid to be zero */ 
 for (i = 0; i  
 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  

} return;

n = neighbors(i,j); printf ("Number of neighbors to element %d,%d = 
 %d ",i,j,n); 
} /* function to compute an element's neighbors */ 
int neighbors (int i, int j) { 
 FILL IN THE REMAINDER OF THE CODE 
grid th st nd th column column column column column column column row 0 st 0 row nd row 0 row th row 0 row th row

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!