Add solutions for arrays exercise|projects section
This commit is contained in:
parent
1e92ab7424
commit
09ce6fc2c0
5 changed files with 140 additions and 6 deletions
40
c_modern_approach/Arrays/exercies/8_1_exercises.c
Normal file
40
c_modern_approach/Arrays/exercies/8_1_exercises.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#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();
|
||||||
|
}
|
38
c_modern_approach/Arrays/exercies/8_2_exercies.c
Normal file
38
c_modern_approach/Arrays/exercies/8_2_exercies.c
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/**
|
||||||
|
* @brief Itreation through array test
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
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(); }
|
32
c_modern_approach/Arrays/projects/1_project.c
Normal file
32
c_modern_approach/Arrays/projects/1_project.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
24
c_modern_approach/Arrays/projects/2_project.c
Normal file
24
c_modern_approach/Arrays/projects/2_project.c
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@
|
||||||
* @param x - pointer to array
|
* @param x - pointer to array
|
||||||
* @param size - size of an 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);
|
printf("Array started with size == %zu\n", size);
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
if (i == size - 1) {
|
if (i == size - 1) {
|
||||||
|
@ -22,11 +22,11 @@ void print_array(int *x, size_t size) {
|
||||||
int main() {
|
int main() {
|
||||||
/* Array initialization */
|
/* Array initialization */
|
||||||
int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
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 */
|
/* Partial array initialization */
|
||||||
int b[N] = {1, 23, 4};
|
int b[N] = {1, 23, 4};
|
||||||
print_array(b, N);
|
print_bool_array(b, N);
|
||||||
|
|
||||||
/* Initialization without size */
|
/* Initialization without size */
|
||||||
int c[] = {10000000, 3, 4};
|
int c[] = {10000000, 3, 4};
|
||||||
|
@ -34,12 +34,12 @@ int main() {
|
||||||
* memory of array and dividing it by size of an element in it
|
* memory of array and dividing it by size of an element in it
|
||||||
*/
|
*/
|
||||||
printf("%lu : %lu\n", sizeof(c), sizeof(c[0]));
|
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 */
|
/* Designated initializers */
|
||||||
int d[N] = {[2] = 15, [9] = 10};
|
int d[N] = {[2] = 15, [9] = 10};
|
||||||
print_array(d, N);
|
print_bool_array(d, N);
|
||||||
|
|
||||||
/* Designated initializers without fixed size */
|
/* Designated initializers without fixed size */
|
||||||
int e[] = {[2] = 15, [9] = 10, [154] = 1};
|
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]));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue