From 09ce6fc2c073ca5c11d94fd92c98b51daa65dc77 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Sun, 8 Dec 2024 23:11:13 +0300 Subject: [PATCH] Add solutions for arrays exercise|projects section --- .../Arrays/exercies/8_1_exercises.c | 40 +++++++++++++++++++ .../Arrays/exercies/8_2_exercies.c | 38 ++++++++++++++++++ c_modern_approach/Arrays/projects/1_project.c | 32 +++++++++++++++ c_modern_approach/Arrays/projects/2_project.c | 24 +++++++++++ c_modern_approach/Arrays/theory.c | 12 +++--- 5 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 c_modern_approach/Arrays/exercies/8_1_exercises.c create mode 100644 c_modern_approach/Arrays/exercies/8_2_exercies.c create mode 100644 c_modern_approach/Arrays/projects/1_project.c create mode 100644 c_modern_approach/Arrays/projects/2_project.c diff --git a/c_modern_approach/Arrays/exercies/8_1_exercises.c b/c_modern_approach/Arrays/exercies/8_1_exercises.c new file mode 100644 index 0000000..e789050 --- /dev/null +++ b/c_modern_approach/Arrays/exercies/8_1_exercises.c @@ -0,0 +1,40 @@ +#include +#include +#define N 7 +#define FN 40 + +void print_bool_array(bool *x, size_t size) { + printf("Array started with size == %zu\n", size); + for (int i = 0; i < size; i++) { + if (i == size - 1) { + printf("%d\nArray ended\n", x[i]); + } else { + printf("%d, ", x[i]); + } + } +} + +void print_int_array(int *x, size_t size) { + printf("Array started with size == %zu\n", size); + for (int i = 0; i < size; i++) { + if (i == size - 1) { + printf("%d\nArray ended\n", x[i]); + } else { + printf("%d, ", x[i]); + } + } +} + +void create_fibonacci_numbers() { + int fin_numbers[FN] = {1, 1}; + for (int i = 2; i < FN; i++) { + fin_numbers[i] = fin_numbers[i - 1] + fin_numbers[i - 2]; + } + print_int_array(fin_numbers, FN); +} + +int main() { + bool weekend[N] = {[0] = true, [N - 1] = true}; + print_bool_array(weekend, N); + create_fibonacci_numbers(); +} diff --git a/c_modern_approach/Arrays/exercies/8_2_exercies.c b/c_modern_approach/Arrays/exercies/8_2_exercies.c new file mode 100644 index 0000000..4d7d233 --- /dev/null +++ b/c_modern_approach/Arrays/exercies/8_2_exercies.c @@ -0,0 +1,38 @@ +/** + * @brief Itreation through array test + */ +#include +void temps() { + int total_temp; + const int temperature_readings[24][30] = { + {25, 31, 19, 27, 22, 34, 28, 20, 33, 24, 29, 21, 26, 32, 23, + 30, 35, 18, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, + {41, 28, 34, 22, 36, 19, 31, 26, 43, 30, 24, 37, 20, 33, 27, + 35, 23, 29, 32, 25, 38, 21, 40, 42, 44, 45, 46, 47, 48, 49}, + {18, 33, 27, 39, 24, 43, 31, 35, 20, 26, 29, 22, 36, 28, 34, + 21, 25, 30, 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 19}, + {35, 22, 29, 41, 26, 31, 20, 33, 27, 24, 36, 28, 34, 21, 25, + 30, 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 19, 23, 38}, + {27, 34, 21, 25, 30, 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 19, 23, 38, 35, 22, 29, 41, 26, 31, 20, 33, 24, 36, 28}, + {40, 41, 42, 43, 44, 45, 46, 47, 48, 19, 23, 38, 35, 22, 29, + 41, 26, 31, 20, 33, 27, 24, 36, 28, 34, 21, 25, 30, 32, 37}, + {46, 47, 48, 19, 23, 38, 35, 22, 29, 41, 26, 31, 20, 33, 27, + 24, 36, 28, 34, 21, 25, 30, 32, 37, 40, 41, 42, 43, 44, 45}, + {19, 23, 38, 35, 22, 29, 41, 26, 31, 20, 33, 27, 24, 36, 28, + 34, 21, 25, 30, 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48}, + {38, 35, 22, 29, 41, 26, 31, 20, 33, 27, 24, 36, 28, 34, 21, + 25, 30, 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 19, 23}, + {22, 29, 41, 26, 31, 20, 33, 27, 24, 36, 28, 34, 21, 25, 30, + 32, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 19, 23, 38, 35}}; + for (int i = 0; i < 24; i++) { + printf("Row: %d\n", i); + for (int j = 0; j < 30; j++) { + printf("\tCell %d,value: %d\n", j, temperature_readings[i][j]); + total_temp += temperature_readings[i][j]; + } + } + float average_temp = (float)total_temp / (24 * 30); + printf("Average temp: %.2f", average_temp); +} +int main() { temps(); } diff --git a/c_modern_approach/Arrays/projects/1_project.c b/c_modern_approach/Arrays/projects/1_project.c new file mode 100644 index 0000000..db36b1d --- /dev/null +++ b/c_modern_approach/Arrays/projects/1_project.c @@ -0,0 +1,32 @@ +#include +#include +int main() { + bool digit_seen[10] = {false}; + int seen_digits[10]; + int iterator = 0; + int digit; + long n; + + printf("Enter a number: "); + scanf("%ld", &n); + + while (n > 0) { + digit = n % 10; + if (digit_seen[digit]) { + seen_digits[iterator] = digit; + iterator += 1; + } + digit_seen[digit] = true; + n /= 10; + } + + if (iterator > 0) { + printf("Repeated digit(s): "); + for (int i = iterator - 1; i >= 0; i--) { + printf("%d ", seen_digits[i]); + } + printf("\n"); + } else { + printf("No repeated digit\n"); + } +} diff --git a/c_modern_approach/Arrays/projects/2_project.c b/c_modern_approach/Arrays/projects/2_project.c new file mode 100644 index 0000000..30f27ff --- /dev/null +++ b/c_modern_approach/Arrays/projects/2_project.c @@ -0,0 +1,24 @@ +#include +#include +int main() { + int occurence_counter[2][10] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}; + long n; + int digit; + printf("Enter a number: "); + scanf("%ld", &n); + + while (n > 0) { + digit = n % 10; + occurence_counter[1][digit] += 1; + n /= 10; + } + + printf("Digit:\t\t"); + for (int i = 0; i < 10; i++) { + printf("%d ", occurence_counter[0][i]); + } + printf("\nOccurrences:\t"); + for (int i = 0; i < 10; i++) { + printf("%d ", occurence_counter[1][i]); + } +} diff --git a/c_modern_approach/Arrays/theory.c b/c_modern_approach/Arrays/theory.c index feddea2..55a6308 100644 --- a/c_modern_approach/Arrays/theory.c +++ b/c_modern_approach/Arrays/theory.c @@ -8,7 +8,7 @@ * @param x - pointer to array * @param size - size of an array */ -void print_array(int *x, size_t size) { +void print_bool_array(int *x, size_t size) { printf("Array started with size == %zu\n", size); for (int i = 0; i < size; i++) { if (i == size - 1) { @@ -22,11 +22,11 @@ void print_array(int *x, size_t size) { int main() { /* Array initialization */ int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - print_array(a, N); + print_bool_array(a, N); /* Partial array initialization */ int b[N] = {1, 23, 4}; - print_array(b, N); + print_bool_array(b, N); /* Initialization without size */ int c[] = {10000000, 3, 4}; @@ -34,12 +34,12 @@ int main() { * memory of array and dividing it by size of an element in it */ printf("%lu : %lu\n", sizeof(c), sizeof(c[0])); - print_array(c, sizeof(c) / sizeof(c[0])); + print_bool_array(c, sizeof(c) / sizeof(c[0])); /* Designated initializers */ int d[N] = {[2] = 15, [9] = 10}; - print_array(d, N); + print_bool_array(d, N); /* Designated initializers without fixed size */ int e[] = {[2] = 15, [9] = 10, [154] = 1}; - print_array(e, sizeof(e) / sizeof(e[0])); + print_bool_array(e, sizeof(e) / sizeof(e[0])); }