Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define rows 5
- #define cols 5
- void calculateCounts(char arr[rows][cols], char newArr[rows][cols], int i, int j) {
- if (i >= rows || j >= cols)
- return;
- if (arr[i][j] == '#') {
- newArr[i][j] = '#';
- }
- else {
- int count = 0;
- if (i > 0 && arr[i - 1][j] == '#')
- count++;
- if (i < rows - 1 && arr[i + 1][j] == '#')
- count++;
- if (j > 0 && arr[i][j - 1] == '#')
- count++;
- if (j < cols - 1 && arr[i][j + 1] == '#')
- count++;
- newArr[i][j] = count + '0';
- }
- if (j == cols - 1) {
- calculateCounts(arr, newArr, i + 1, 0);
- }
- else {
- calculateCounts(arr, newArr, i, j + 1);
- }
- }
- int main() {
- char arr[rows][cols] = {
- {'0', '0', '#', '0', '#'},
- {'0', '0', '#', '0', '0'},
- {'0', '0', '0', '0', '#'},
- {'0', '0', '#', '0', '0'},
- {'0', '#', '#', '0', '#'}
- };
- char newArr[rows][cols];
- calculateCounts(arr, newArr, 0, 0);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- printf("%c ", newArr[i][j]);
- }
- puts("");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement