fork download
  1. #include <stdio.h>
  2.  
  3. void neighbour(int sx, int sy);
  4. void diagnolNeighbour(int sx, int sy);
  5. void allNeighbour(int sx, int sy);
  6. void display();
  7.  
  8. int array[4][4];
  9. int row = 3;
  10. int col = 3;
  11. int startRow = 0;
  12. int startCol = 0;
  13.  
  14. int main()
  15. {
  16. int k = 0;
  17. int i = 0;
  18. int j = 0;
  19.  
  20. for (i = 0; i <= row; i++) {
  21. for (j = 0; j <= col; j++) {
  22. ++k;
  23. array[i][j] = k;
  24. if (k < 10) {
  25. printf("%d ", array[i][j]);
  26. } else {
  27. printf("%d ", array[i][j]);
  28. }
  29. }
  30. printf("\n");
  31. }
  32. printf("\n\n");
  33.  
  34. for (i = 0; i <= row; i++) {
  35. for (j = 0; j <= col; j++) {
  36. printf("\nNeighbour for (%d,%d) %d: ",i,j, array[i][j]);
  37. neighbour(i,j);
  38. }
  39. }
  40.  
  41. display();
  42. printf("Neighbour with Diagnols\n");
  43. for (i = 0; i <= row; i++) {
  44. for (j = 0; j <= col; j++) {
  45. printf("\nNeighbour for (%d,%d) %d: ",i,j, array[i][j]);
  46. diagnolNeighbour(i,j);
  47. }
  48. }
  49.  
  50. display();
  51. printf("Neighbour with all Diagnols\n");
  52. for (i = 0; i <= row; i++) {
  53. for (j = 0; j <= col; j++) {
  54. printf("\nNeighbour for (%d,%d) %d: ",i,j, array[i][j]);
  55. allNeighbour(i,j);
  56. }
  57. }
  58. printf("\n");
  59.  
  60. return 0;
  61. }
  62.  
  63. void display()
  64. {
  65. int k = 0;
  66. int i = 0;
  67. int j = 0;
  68.  
  69. printf("\n\n");
  70. for (i = 0; i <= row; i++) {
  71. for (j = 0; j <= col; j++) {
  72. ++k;
  73. if (k < 10) {
  74. printf("%d ", array[i][j]);
  75. } else {
  76. printf("%d ", array[i][j]);
  77. }
  78. }
  79. printf("\n");
  80. }
  81. }
  82.  
  83. /******************************************************
  84.  * Function: neighbour(int sx, int sy)
  85.  * This function returns the neighbour of sx, sy
  86.  * For e.g.
  87.  * 1 2 3
  88.  * 4 5 6
  89.  * 7 8 9
  90.  *
  91.  * Neighbour for 5 (1,1) = 4, 2, 6, 8
  92.  * Neighbour for 1 (0,0) = 4, 2
  93.  * ****************************************************/
  94. void neighbour(int sx, int sy)
  95. {
  96. if (sy - 1 >= startCol && sy - 1 <= col) {
  97. printf("\n[%d][%d]: %d ", sx, sy - 1, array[sx][sy - 1]);
  98. }
  99.  
  100. if (sx - 1 >= startRow && sx - 1 <= row) {
  101. printf("\n[%d][%d]: %d ", sx - 1, sy, array[sx - 1][sy]);
  102. }
  103.  
  104. if (sy + 1 >= startCol && sy + 1 <= col) {
  105. printf("\n[%d][%d]: %d ", sx, sy + 1, array[sx][sy + 1]);
  106. }
  107.  
  108. if (sx + 1 >= startRow && sx + 1 <= row) {
  109. printf("\n[%d][%d]: %d ", sx + 1, sy,array[sx + 1][sy]);
  110. }
  111. }
  112.  
  113. /******************************************************
  114.  * Function: diagnolNeighbour(int sx, int sy)
  115.  * This function returns the neighbour of sx, sy and
  116.  * diagonal neighbours too.
  117.  * For e.g.
  118.  * 1 2 3
  119.  * 4 5 6
  120.  * 7 8 9
  121.  *
  122.  * Neighbour for 5 (1,1) = 4, 2, 6, 8, 1, 3, 7, 9
  123.  * Neighbour for 1 (0,0) = 4, 2, 5
  124.  * ****************************************************/
  125. void diagnolNeighbour(int sx, int sy) {
  126. if (sy - 1 >= startCol && sy - 1 <= col) {
  127. printf("%d ", array[sx][sy - 1]);
  128. }
  129.  
  130. if (sx - 1 >= startRow && sx - 1 <= row) {
  131. printf("%d ", array[sx - 1][sy]);
  132.  
  133. //Diagnols
  134. if (sy - 1 >= startCol && sy - 1 <= col) {
  135. printf("%d ", array[sx - 1][sy - 1]);
  136. }
  137. if (sy + 1 >= startCol && sy + 1 <= col) {
  138. printf("%d ", array[sx - 1][sy + 1]);
  139. }
  140. }
  141.  
  142. if (sy + 1 >= startCol && sy + 1 <= col) {
  143. printf("%d ", array[sx][sy + 1]);
  144. }
  145.  
  146. if (sx + 1 >= startRow && sx + 1 <= row) {
  147. printf("%d ", array[sx + 1][sy]);
  148.  
  149. //Diagnols
  150. if (sy - 1 >= startCol && sy - 1 <= col) {
  151. printf("%d ", array[sx + 1][sy - 1]);
  152. }
  153. if (sy + 1 >= startCol && sy + 1 <= col) {
  154. printf("%d ", array[sx + 1][sy + 1]);
  155. }
  156. }
  157. }
  158.  
  159. /******************************************************
  160.  * Function: allNeighbour(int sx, int sy)
  161.  * This function returns all the neighbour of sx, sy and
  162.  * all diagonal neighbours.
  163.  * For e.g.
  164.  * 1 2 3 4
  165.  * 5 6 7 8
  166.  * 9 10 11 12
  167.  * 13 14 15 16
  168.  *
  169.  * Neighbour for 5 (1,0) = 1, 9, 13, 6, 7, 8, 10, 15
  170.  * Neighbour for 11 (2,2) = 3, 7, 15, 9, 10, 12, 1, 6, 16
  171.  * ****************************************************/
  172. void allNeighbour(int sx, int sy)
  173. {
  174. int done = 0;
  175. int tempX = sx;
  176. int tempY = sy;
  177. int i = 0;
  178. int j = 0;
  179.  
  180. // Get all the elements in row
  181. for (i = 0; i <= row; i++) {
  182. if (array[i][sy] == array[sx][sy]) {
  183. continue;
  184. }
  185. printf("%d ", array[i][sy]);
  186. }
  187.  
  188. // Get all the elements in col
  189. for (j = 0; j <= col; j++) {
  190. if (array[sx][j] == array[sx][sy]) {
  191. continue;
  192. }
  193. printf("%d ", array[sx][j]);
  194. }
  195.  
  196. // Diagnols
  197. while (1) {
  198. tempX--; tempY--;
  199. if (tempX >= startRow && tempY >= startCol) {
  200. printf("%d ", array[tempX][tempY]);
  201. } else {
  202. done = 1;
  203. }
  204.  
  205. sx++,sy++;
  206. if (sx <= row && sy <= col) {
  207. printf("%d ", array[sx][sy]);
  208. } else if (done) {
  209. break;
  210. }
  211. }
  212. }
  213.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  



Neighbour for (0,0) 1: 
[0][1]: 2 
[1][0]: 5 
Neighbour for (0,1) 2: 
[0][0]: 1 
[0][2]: 3 
[1][1]: 6 
Neighbour for (0,2) 3: 
[0][1]: 2 
[0][3]: 4 
[1][2]: 7 
Neighbour for (0,3) 4: 
[0][2]: 3 
[1][3]: 8 
Neighbour for (1,0) 5: 
[0][0]: 1 
[1][1]: 6 
[2][0]: 9 
Neighbour for (1,1) 6: 
[1][0]: 5 
[0][1]: 2 
[1][2]: 7 
[2][1]: 10 
Neighbour for (1,2) 7: 
[1][1]: 6 
[0][2]: 3 
[1][3]: 8 
[2][2]: 11 
Neighbour for (1,3) 8: 
[1][2]: 7 
[0][3]: 4 
[2][3]: 12 
Neighbour for (2,0) 9: 
[1][0]: 5 
[2][1]: 10 
[3][0]: 13 
Neighbour for (2,1) 10: 
[2][0]: 9 
[1][1]: 6 
[2][2]: 11 
[3][1]: 14 
Neighbour for (2,2) 11: 
[2][1]: 10 
[1][2]: 7 
[2][3]: 12 
[3][2]: 15 
Neighbour for (2,3) 12: 
[2][2]: 11 
[1][3]: 8 
[3][3]: 16 
Neighbour for (3,0) 13: 
[2][0]: 9 
[3][1]: 14 
Neighbour for (3,1) 14: 
[3][0]: 13 
[2][1]: 10 
[3][2]: 15 
Neighbour for (3,2) 15: 
[3][1]: 14 
[2][2]: 11 
[3][3]: 16 
Neighbour for (3,3) 16: 
[3][2]: 15 
[2][3]: 12 

1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  
Neighbour with Diagnols

Neighbour for (0,0) 1: 2 5 6 
Neighbour for (0,1) 2: 1 3 6 5 7 
Neighbour for (0,2) 3: 2 4 7 6 8 
Neighbour for (0,3) 4: 3 8 7 
Neighbour for (1,0) 5: 1 2 6 9 10 
Neighbour for (1,1) 6: 5 2 1 3 7 10 9 11 
Neighbour for (1,2) 7: 6 3 2 4 8 11 10 12 
Neighbour for (1,3) 8: 7 4 3 12 11 
Neighbour for (2,0) 9: 5 6 10 13 14 
Neighbour for (2,1) 10: 9 6 5 7 11 14 13 15 
Neighbour for (2,2) 11: 10 7 6 8 12 15 14 16 
Neighbour for (2,3) 12: 11 8 7 16 15 
Neighbour for (3,0) 13: 9 10 14 
Neighbour for (3,1) 14: 13 10 9 11 15 
Neighbour for (3,2) 15: 14 11 10 12 16 
Neighbour for (3,3) 16: 15 12 11 

1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  
Neighbour with all Diagnols

Neighbour for (0,0) 1: 5 9 13 2 3 4 6 11 16 
Neighbour for (0,1) 2: 6 10 14 1 3 4 7 12 
Neighbour for (0,2) 3: 7 11 15 1 2 4 8 
Neighbour for (0,3) 4: 8 12 16 1 2 3 
Neighbour for (1,0) 5: 1 9 13 6 7 8 10 15 
Neighbour for (1,1) 6: 2 10 14 5 7 8 1 11 16 
Neighbour for (1,2) 7: 3 11 15 5 6 8 2 12 
Neighbour for (1,3) 8: 4 12 16 5 6 7 3 
Neighbour for (2,0) 9: 1 5 13 10 11 12 14 
Neighbour for (2,1) 10: 2 6 14 9 11 12 5 15 
Neighbour for (2,2) 11: 3 7 15 9 10 12 6 16 1 
Neighbour for (2,3) 12: 4 8 16 9 10 11 7 2 
Neighbour for (3,0) 13: 1 5 9 14 15 16 
Neighbour for (3,1) 14: 2 6 10 13 15 16 9 
Neighbour for (3,2) 15: 3 7 11 13 14 16 10 5 
Neighbour for (3,3) 16: 4 8 12 13 14 15 11 6 1