I'm writing a function 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 ith, jth element of the array.
I have gotten to this point with my code. Can someone give me a pointer on how to continue with the function? I'm assuming I'll be using if statements but I'm not sure how.
Code:
#include<stdio.h>
#define SIZE 7
int grid[SIZE][SIZE];
int neighbors (int i,int j);
void main ()
{
int i, j, n;
/* initializing the entire grid to be zero */
for(i =0; i < SIZE; i++)
for(j =0; j < SIZE; j++)
grid[i][j]=0;
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\n",i,j,n);
}
return;
}
/* function to compute an element's neighbors */
int neighbors (int i,int j)
{