Advertisement
Guest User

neighbors program

a guest
Feb 20th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. /* this program finds the elements in a two-dimensional array that are larger than all of their neighbors.
  2.  
  3. So if my input is
  4.  
  5. 3 2 9
  6.  
  7. 13 7 6
  8.  
  9. 1 5 8
  10.  
  11. It should print
  12.  
  13. 9
  14.  
  15. 13
  16.  
  17. 8
  18.  
  19. */
  20.  
  21. #include <simpio.h>
  22. #include "stdio.h"
  23. #include "genlib.h"
  24.  
  25. #define n 3
  26.  
  27. void initArray(int a[n][n]);
  28. void widenArray(int a[n][n]);
  29. bool neighbor(int a[n][n], int m, int p);
  30.  
  31. main()
  32. {
  33.       int a[n][n], i, j;
  34.       initArray(a);
  35.       widenArray(a);
  36.       for(i=0;i<n;i++)
  37.       {
  38.                       for(j=0;j<n;j++)
  39.                       {
  40.                                       if(neighbor(a, i, j)) printf("%d", a[i][j]);
  41.                       }
  42.       }
  43.      
  44.                                      
  45.       getchar();
  46. }
  47.  
  48.  
  49.  
  50. void initArray(int a[n][n])
  51. {
  52.      int i, j;
  53.      for(i=0;i<n;i++)
  54.      {
  55.                      for(j=0;j<n;j++)
  56.                      {
  57.                            printf("a[%d][%d]= ",i,j);
  58.                            a[i][j]=GetInteger();
  59.                      }
  60.                      printf("\n");
  61.      }
  62. }
  63.  
  64.  
  65. void widenArray(int a[n][n])
  66. {
  67.      int i, j;
  68.      a[n][n]=a[n+2][n+2];
  69.      for(i=0;i<n;i++) /* This loop widens the array dimensions to n+2 and pushes the current defined values of the array to the "middle" quadrants of the array */
  70.      {
  71.                      for(j=0;j<n;j++)
  72.                      {
  73.                                      a[i+1][j+1]=a[i][j];
  74.                                      }
  75.      }
  76.      /* The following two loops define the outland array values to -1, so we can have the same neighbor algorithm for all the middle (defined) values  -- assuming the user only enters positive integers. We could define it to be, say -399, if the user wanted to enter integers from -398 on up. */
  77.      
  78.      /* verticle */
  79.      for(i=0;i<(n+2);i++)
  80.      {
  81.                          a[i][0]=-1;
  82.                          a[i][n+2]=-1;
  83.      }
  84.      
  85.      /* horizontal */
  86.      for(j=0;j<(n+2);j++)
  87.      {
  88.                          a[0][j]=-1;
  89.                          a[n+2][j]=-1;
  90.      }
  91. }
  92.  
  93.  
  94.  
  95. bool neighbor(int a[n][n], int m, int p)
  96. {
  97.      int i, j;
  98.      for(i=m-1;i<=m+1;i++)
  99.      {
  100.                           for(j=p-1;j<=p+1;j++)
  101.                           {
  102.                                                if(a[i][j]>a[m][n]) return FALSE;
  103.                                                }
  104.      }
  105.      return TRUE;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement