Add some exercises for 11 section
This commit is contained in:
parent
2980a58784
commit
2cc0db7791
3 changed files with 100 additions and 0 deletions
22
c_modern_approach/Pointers/exercises/e_4.c
Normal file
22
c_modern_approach/Pointers/exercises/e_4.c
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Variable swapping exercide
|
||||||
|
*/
|
||||||
|
void swap(int *p, int *q) {
|
||||||
|
printf("*p: %d; p = %p\n", *p, p);
|
||||||
|
printf("*q: %d; q = %p\n", *q, q);
|
||||||
|
int x = *p;
|
||||||
|
int y = *q;
|
||||||
|
*p = y;
|
||||||
|
*q = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a, b;
|
||||||
|
a = 4;
|
||||||
|
b = 5;
|
||||||
|
printf("a = %d, b = %d\n", a, b);
|
||||||
|
swap(&a, &b);
|
||||||
|
printf("a = %d, b = %d", a, b);
|
||||||
|
}
|
35
c_modern_approach/Pointers/exercises/e_5.c
Normal file
35
c_modern_approach/Pointers/exercises/e_5.c
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Splitting seconds into hh:mm:ss */
|
||||||
|
|
||||||
|
void split_time(long total_sec, int *hr, int *min, int *sec) {
|
||||||
|
int f_h, f_m, f_s;
|
||||||
|
f_h = total_sec / 3600;
|
||||||
|
printf("hours: %d\n", f_h);
|
||||||
|
total_sec = total_sec - f_h * 3600;
|
||||||
|
f_m = total_sec / 60;
|
||||||
|
printf("minutes: %d\n", f_m);
|
||||||
|
total_sec = total_sec - f_m * 60;
|
||||||
|
printf("seconds: %ld\n", total_sec);
|
||||||
|
*hr = f_h;
|
||||||
|
*min = f_m;
|
||||||
|
*sec = total_sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
void split_time_otimized(long total_sec, int *hr, int *min, int *sec) {
|
||||||
|
*hr = (int)(total_sec / 3600);
|
||||||
|
*min = (int)((total_sec % 3600) / 60);
|
||||||
|
*sec = (int)(total_sec % 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
long total_sec = 5123;
|
||||||
|
int hours, minutes, seconds;
|
||||||
|
hours = 0;
|
||||||
|
minutes = 0;
|
||||||
|
seconds = 0;
|
||||||
|
printf("BEFORE: %d:%d:%d\n", hours, minutes, seconds);
|
||||||
|
/* split_time(total_sec, &hours, &minutes, &seconds); */
|
||||||
|
split_time_otimized(total_sec, &hours, &minutes, &seconds);
|
||||||
|
printf("AFTER: %d:%d:%d\n", hours, minutes, seconds);
|
||||||
|
}
|
43
c_modern_approach/Pointers/exercises/e_6.c
Normal file
43
c_modern_approach/Pointers/exercises/e_6.c
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/* Finding two largest values in array */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define MAX_RANDOM_VALUE 1000
|
||||||
|
#define ARRAY_SIZE 10
|
||||||
|
|
||||||
|
void find_two_largest(int a[], int n, int *largest, int *second_largest) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (a[i] > *largest) {
|
||||||
|
*second_largest = *largest;
|
||||||
|
*largest = a[i];
|
||||||
|
} else if (a[i] < *largest && a[i] > *second_largest) {
|
||||||
|
*second_largest = a[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int random_numbers_array[ARRAY_SIZE];
|
||||||
|
int largest = 0;
|
||||||
|
int second_largest = 0;
|
||||||
|
// Generate random numbers
|
||||||
|
srand(time(NULL));
|
||||||
|
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||||
|
random_numbers_array[i] = rand() % MAX_RANDOM_VALUE + 1;
|
||||||
|
}
|
||||||
|
// Print array
|
||||||
|
printf("[");
|
||||||
|
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||||
|
if (i == ARRAY_SIZE - 1) {
|
||||||
|
printf("%d", random_numbers_array[i]);
|
||||||
|
} else {
|
||||||
|
printf("%d,", random_numbers_array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
// Exercise
|
||||||
|
find_two_largest(random_numbers_array, ARRAY_SIZE, &largest, &second_largest);
|
||||||
|
printf("Largest: %d\nSecond largest: %d\n", largest, second_largest);
|
||||||
|
}
|
Loading…
Reference in a new issue