Thread: Function for my grid array

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    2

    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][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)
      {
    
    
    

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    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.

    (edited to add)
    There would be eight neighbors if you are counting both orthagonal and diagonal neighbors. Only four if you are just counting orthagonal ones.

    -
    Last edited by megafiddle; 03-25-2016 at 12:24 AM.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    2
    How would I go on to putting that in my code? Do you recommend doing an if statement with [i -1][j]?

    Thanks for your help

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    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.

    -
    Last edited by megafiddle; 03-25-2016 at 03:18 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    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).

    -
    Last edited by megafiddle; 03-25-2016 at 03:39 AM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    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][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 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++;
        }
    }
    
    return total;
    }

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by nerio View Post
    In English language it is called orthogonal.
    In England maybe.

    In Virginia it is orthagonal

    I should have mentioned something about this code:
    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. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    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 subscribe to a feed

Similar Threads

  1. Suduko Grid For Solving From Already Generated Grid Function
    By Nick Krause in forum C Programming
    Replies: 21
    Last Post: 11-17-2014, 03:16 PM
  2. Drawing a grid over an array
    By legase in forum C Programming
    Replies: 5
    Last Post: 01-03-2013, 05:51 AM
  3. Generic Function to show grid.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 07-20-2011, 12:41 PM
  4. Printing an 4x4 grid and storing it in an array?
    By Watabou in forum C Programming
    Replies: 10
    Last Post: 03-02-2011, 03:03 AM
  5. array/grid qustion
    By razzaz in forum C++ Programming
    Replies: 12
    Last Post: 10-20-2008, 07:38 AM

Tags for this Thread