diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-02-09 23:57:00 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-02-09 23:57:00 +0330 |
| commit | 7eb844e25c990401d9fde7e3af9fe77813cbc32a (patch) | |
| tree | 044bcb6888e9ff9e94383528feae045dc8a3a8d5 /challenge-088 | |
| parent | 232ddad947cd79c1f58187643a51055d3c8bf030 (diff) | |
| download | perlweeklychallenge-club-7eb844e25c990401d9fde7e3af9fe77813cbc32a.tar.gz perlweeklychallenge-club-7eb844e25c990401d9fde7e3af9fe77813cbc32a.tar.bz2 perlweeklychallenge-club-7eb844e25c990401d9fde7e3af9fe77813cbc32a.zip | |
Solutions to challenge 88.
Diffstat (limited to 'challenge-088')
| -rw-r--r-- | challenge-088/deadmarshal/C/ch-1.c | 53 | ||||
| -rw-r--r-- | challenge-088/deadmarshal/C/ch-2.c | 57 | ||||
| -rw-r--r-- | challenge-088/deadmarshal/README | 1 |
3 files changed, 111 insertions, 0 deletions
diff --git a/challenge-088/deadmarshal/C/ch-1.c b/challenge-088/deadmarshal/C/ch-1.c new file mode 100644 index 0000000000..b67f148411 --- /dev/null +++ b/challenge-088/deadmarshal/C/ch-1.c @@ -0,0 +1,53 @@ +#include<stdio.h> +#include<stdlib.h> +#include<assert.h> + +int *array_of_product(const int *arr, size_t sz) +{ + int i,j; + int *result = malloc(sz * sizeof(int)); + assert(result && "Allocation failed!\n"); + + for(i = 0; i < sz; i++) + { + int prod = 1; + for(j = 0; j < sz; j++) + { + if(i != j) + { + prod *= arr[j]; + result[i] = prod; + } + } + } + + return result; +} + +int main(void) +{ + int arr[] = {5, 2, 1, 4, 3}; + int arr2[] = {2, 1, 4, 3}; + + size_t n = 5; + size_t n2 = 4; + + int *result = array_of_product(arr, n); + int *result2 = array_of_product(arr2, n2); + + for(int i = 0; i < n; i++) + printf("%d ", result[i]); + printf("\n"); + + for(int i = 0; i < n2; i++) + printf("%d ", result2[i]); + printf("\n"); + + + if(result) + free(result); + if(result2) + free(result2); + + return 0; +} diff --git a/challenge-088/deadmarshal/C/ch-2.c b/challenge-088/deadmarshal/C/ch-2.c new file mode 100644 index 0000000000..068f50ff2a --- /dev/null +++ b/challenge-088/deadmarshal/C/ch-2.c @@ -0,0 +1,57 @@ +#include<stdio.h> +#include<stdlib.h> +#include<assert.h> + +void spiral_matrix(size_t rows, size_t cols, const int mat[rows][cols]) +{ + int i, k = 0, l = 0; + int last_row = rows-1, last_col = cols-1; + + while(k <= last_row && l <= last_col) + { + for(i = l; i <= last_col; i++) + printf("%d ", mat[k][i]); + k++; + + for(i = k; i <= last_row; i++) + printf("%d ", mat[i][last_col]); + last_col--; + + if(k <= last_row) + { + for(i = last_col; i >= l; i--) + printf("%d ", mat[last_row][i]); + last_row--; + } + if(l <= last_col) + { + for(i = last_row; i >= k; i--) + printf("%d ", mat[i][l]); + l++; + } + } +} + +int main(void) +{ + + int arr[3][3] = { + { 1, 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9 } + }; + + int arr2[4][4] = { + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 9, 10, 11, 12 }, + { 13, 14, 15, 16 } + }; + + spiral_matrix(3, 3, arr); + puts(""); + spiral_matrix(4, 4, arr2); + puts(""); + + return 0; +} diff --git a/challenge-088/deadmarshal/README b/challenge-088/deadmarshal/README new file mode 100644 index 0000000000..bd0573c85f --- /dev/null +++ b/challenge-088/deadmarshal/README @@ -0,0 +1 @@ +Solution by Ali Moradi |
