# Thread: Function for my grid array

1. ## Function for my grid array

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;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=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)
{

``` 2. An element, grid[i][j], has up to eight possible neighbors. Each of these neighboring elements can be referenced by adding an offset to i and j.

For example, the element directly above [i][j] is [i -1][j]; the element to the right of [i][j] is [i][j + 1].
(I am assuming i represents a row value and j represents a column value)

So you can reference all eight neighbors by using the appropriate +1 and -1 offsets for i and j.

However, the elements along the edges of the grid do not have all eight neighbors. One way handle these elements is to test if i or j is along an edge and only look at the appropriate neighbors. This not the best way as it results in nine different conditions that need to be handled.

A better method is to just look at the offset'd values of i and j and see if they lie within the grid. If so, then you count them.

There would be eight neighbors if you are counting both orthagonal and diagonal neighbors. Only four if you are just counting orthagonal ones.

- 3. How would I go on to putting that in my code? Do you recommend doing an if statement with [i -1][j]? 4. You mentioned adjacent elements, so I am assuming only orthagonal neighbors.

There are two conditions to test:

1) that the neighboring element is within the grid, and
2) that the neighboring element is nonzero

So in the function, you would need a variable for the total value initialized to 0, and four sets of logical tests, one for each of the four neighbors.

For example, starting with the top neighbor:
Code:
```if( (i-1) >= 0 && (i-1) < SIZE)    // test if top neighbor is within grid
if(grid[i-1][j])    // test if neighbor is nonzero
total = total + 1;```
And then you do the same thing for each of the other three neighbors, using the appropriate offsets.
Offset i to get the neighbor above or below, and offset j to get the neighbor to the left or right.

- 5. I just realized, that could be simplified.

Since i must be within the grid, i-1 cannot be below the grid, so there is no reason to test that it is less than SIZE.

So all you need for the top neighbor:
Code:
```if( (i-1) >= 0 )    // test if top neighbor is within grid
if(grid[i-1][j])    // test if neighbor is nonzero
total = total + 1;```
You could also combine the two tests:
Code:
```if( (i-1) >= 0 && grid[i-1][j])    // test if top neighbor is within grid AND neighbor is nonzero
total = total + 1;```
When examining the bottom neighbor, you would of course compare it to the SIZE boundary (grid bottom), not to 0 (grid top).

- 6. In English language it is called orthogonal.

https://en.wikipedia.org/wiki/Orthogonality

Code:
```#include <stdio.h>
#include <stdint.h>

#define SIZE 7
int grid[SIZE][SIZE];

int neighbors (int i,int j);

int 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;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=1;
grid=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 0;
}

/* function to compute an element's neighbors */
int neighbors (int i,int j)
{
int total=0;

if(((i-1)>=0) && ((i-1)<=SIZE))
{
if((grid[i-1][j]!=0))
{
total++;
}
}

if(((i+1)>=0) && ((i+1)<=SIZE))
{
if((grid[i+1][j]!=0))
{
total++;
}
}

if(((j-1)>=0) && ((j-1)<=SIZE))
{
if((grid[i][j-1]!=0))
{
total++;
}
}

if(((j+1)>=0) && ((j+1)<=SIZE))
{
if((grid[i][j+1]!=0))
{
total++;
}
}

}``` 7. Originally Posted by nerio In English language it is called orthogonal.
In England maybe.

In Virginia it is orthagonal Code:
```if( (i-1) >= 0 && grid[i-1][j])    // test if top neighbor is within grid AND neighbor is nonzero
total = total + 1;```
One should not reference an element outside the array bounds. So one would not want to ever evaluate grid[i-1][j] if i were zero. In that case grid[-1][j] would refer to a value at an invalid address.

This is not a problem in the code just above due to what is called "short circuit evaluation". Once the truth or falsehood of an expression is determined, the remainder of the sub-expressions will not be evaluated. This guarantees that grid[i-1][j] will only be evaluated if i-1 is >= 0.

The order of the two sub-expressions is important though. This would cause problems:
Code:
```if( grid[i-1][j] && (i-1) >= 0 )
total = total + 1;```
- 8. Another possibility:
Code:
```int neighbors(int i, int j) {
int cnt = 0;
if (i > 0)      cnt += grid[i-1][j];
if (i < SIZE-1) cnt += grid[i+1][j];
if (j > 0)      cnt += grid[i][j-1];
if (j < SIZE-1) cnt += grid[i][j+1];
return cnt;
}``` Popular pages Recent additions array, function, grid 