diff --git a/.gitmodules b/.gitmodules index acdd8ae..350734e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "STEP1_Basics/3_Formatting/c_cross_pack"] path = STEP1_Basics/3_Formatting/c_cross_pack url = ssh://git@git.del.pw:2222/justuser-31/c_cross_pack.git +[submodule "STEP1_Basics/5_Recursion/c_cross_pack"] + path = STEP1_Basics/5_Recursion/c_cross_pack + url = ssh://git@git.del.pw:2222/justuser-31/c_cross_pack.git diff --git a/STEP1_Basics/5_Recursion/c_cross_pack b/STEP1_Basics/5_Recursion/c_cross_pack new file mode 160000 index 0000000..0d5a86a --- /dev/null +++ b/STEP1_Basics/5_Recursion/c_cross_pack @@ -0,0 +1 @@ +Subproject commit 0d5a86a1586708ef6e77604b087e825a6f08490f diff --git a/STEP1_Basics/5_Recursion/main.c b/STEP1_Basics/5_Recursion/main.c new file mode 100644 index 0000000..2368204 --- /dev/null +++ b/STEP1_Basics/5_Recursion/main.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include "c_cross_pack/libs/string.h" + +int rmin(int* nums, int size, double i) { + if (i == INT_MIN) { + return rmin(nums, size - 1, nums[size]); + } + // If we at the end + if (size == 0) { + return nums[0]; + } + else { + int next_i = rmin(nums, size - 1, nums[size]); + if (rmin(nums, size - 1, nums[size]) < nums[size]) { + return next_i; + } + else { + return nums[size]; + } + } +} + +int main() { + // Generate seed from now time in microseconds + struct timeval now; + gettimeofday(&now, NULL); + srandom(now.tv_usec); + + string list = strInit(""); + // Generate random nums under 100 + int* nums = malloc(10 * sizeof(int)); + for (int i = 0; i < 10; i++) { + nums[i] = random() % 100; + //printf("%d ", nums[i]); + strAdd(&list, int2Str(&nums[i])); + strAdd(&list, " "); + } + //printf("\n"); + + // Find min + int min = rmin(nums, 10, INT_MIN); + + printf("Original list:\n%s\n" + "\nMin: %d\n", list, min); + + return 0; +}