Advertisement
valve2

# around 0 counter <<2D>>

May 14th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define rows 5
  6. #define cols 5
  7.  
  8. void calculateCounts(char arr[rows][cols], char newArr[rows][cols], int i, int j) {
  9.     if (i >= rows || j >= cols)
  10.         return;
  11.  
  12.     if (arr[i][j] == '#') {
  13.         newArr[i][j] = '#';
  14.     }
  15.     else {
  16.         int count = 0;
  17.  
  18.         if (i > 0 && arr[i - 1][j] == '#')
  19.             count++;
  20.  
  21.         if (i < rows - 1 && arr[i + 1][j] == '#')
  22.             count++;
  23.  
  24.         if (j > 0 && arr[i][j - 1] == '#')
  25.             count++;
  26.  
  27.         if (j < cols - 1 && arr[i][j + 1] == '#')
  28.             count++;
  29.  
  30.         newArr[i][j] = count + '0';
  31.     }
  32.  
  33.     if (j == cols - 1) {
  34.         calculateCounts(arr, newArr, i + 1, 0);
  35.     }
  36.     else {
  37.         calculateCounts(arr, newArr, i, j + 1);
  38.     }
  39. }
  40.  
  41. int main() {
  42.     char arr[rows][cols] = {
  43.         {'0', '0', '#', '0', '#'},
  44.         {'0', '0', '#', '0', '0'},
  45.         {'0', '0', '0', '0', '#'},
  46.         {'0', '0', '#', '0', '0'},
  47.         {'0', '#', '#', '0', '#'}
  48.     };
  49.  
  50.     char newArr[rows][cols];
  51.     calculateCounts(arr, newArr, 0, 0);
  52.  
  53.     for (int i = 0; i < rows; i++) {
  54.         for (int j = 0; j < cols; j++) {
  55.             printf("%c ", newArr[i][j]);
  56.         }
  57.         puts("");
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement