Advertisement
korobushk

sum of the neighbour

Mar 14th, 2021 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. for (int i = 0; i < columnLength; i++) {
  2.             for (int j = 0; j < rowLength; j++) {
  3.                 int up = i + 1;
  4.                 if (up == columnLength) {
  5.                     up = 0;
  6.                 }
  7.                 int down = i - 1;                                          
  8.                 if (down < 0) {
  9.                     down = columnLength - 1;
  10.                 }
  11.                 int right = j + 1;
  12.                 if (right == rowLength) {
  13.                     right = 0;
  14.                 }
  15.                 int left = j - 1;
  16.                 if (left < 0) {
  17.                     left = rowLength - 1;
  18.                 }
  19.  
  20.                 matrix2[i][j] = matrix1[up][j] + matrix1[down][j] + matrix1[i][right] +  matrix1[i][left];
  21.             }
  22.         }
  23.  
  24.  
  25. /* print matrix2 ,
  26.  Sample Input:
  27. 9  5  3
  28. 0  7  -1
  29.  -5  2  9
  30.  
  31.  Sample Output:
  32. 3  21  22
  33.  10  6  19
  34.  20  16  -1 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement