From e1417143f28e2cf79090e9dd89bb11fac7815f67 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Wed, 31 May 2023 09:02:17 +0200 Subject: [PATCH 1/4] coding task 8 --- 8_Power_Calculator/8_Power_Calculator.c | 32 +++++++++++++++++++++++++ 8_Power_Calculator/8_Power_Calculator.h | 5 ++++ 2 files changed, 37 insertions(+) create mode 100644 8_Power_Calculator/8_Power_Calculator.c create mode 100644 8_Power_Calculator/8_Power_Calculator.h diff --git a/8_Power_Calculator/8_Power_Calculator.c b/8_Power_Calculator/8_Power_Calculator.c new file mode 100644 index 0000000..149d214 --- /dev/null +++ b/8_Power_Calculator/8_Power_Calculator.c @@ -0,0 +1,32 @@ +#include "8_Power_Calculator.h" + +void main() { + unsigned long base, power; + + // input + printf("Please type in the base: "); + scanf("%i", &base); + printf("\nPlease type in the power: "); + scanf("%i", &power); + + // calculate + Calculate(&base, &power); + + // output + printf("\nThe number is: %i.", base); + + return 0; +} + +void Calculate(unsigned long* base, unsigned long* power) { + unsigned long baseOriginal = *base; + if (*power == 0) { // power zero + *base = 1; + return; + } + if (*power > 0) { // power positive + for (unsigned int i = 1; i < *power; i++) { + *base *= baseOriginal; + } + } +} diff --git a/8_Power_Calculator/8_Power_Calculator.h b/8_Power_Calculator/8_Power_Calculator.h new file mode 100644 index 0000000..3013847 --- /dev/null +++ b/8_Power_Calculator/8_Power_Calculator.h @@ -0,0 +1,5 @@ +// libraries +#include + +//functions +void Calculate(unsigned long*, unsigned long*); -- 2.52.0 From 5c0b4baf2bbc68250868db064e5b7867d962a91d Mon Sep 17 00:00:00 2001 From: SinusFox Date: Wed, 31 May 2023 09:29:44 +0200 Subject: [PATCH 2/4] removed pointer --- 8_Power_Calculator/8_Power_Calculator.c | 18 ++++++++---------- 8_Power_Calculator/8_Power_Calculator.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/8_Power_Calculator/8_Power_Calculator.c b/8_Power_Calculator/8_Power_Calculator.c index 149d214..9a5b167 100644 --- a/8_Power_Calculator/8_Power_Calculator.c +++ b/8_Power_Calculator/8_Power_Calculator.c @@ -10,7 +10,7 @@ void main() { scanf("%i", &power); // calculate - Calculate(&base, &power); + base = Calculate(base, power); // output printf("\nThe number is: %i.", base); @@ -18,15 +18,13 @@ void main() { return 0; } -void Calculate(unsigned long* base, unsigned long* power) { - unsigned long baseOriginal = *base; - if (*power == 0) { // power zero - *base = 1; - return; - } - if (*power > 0) { // power positive - for (unsigned int i = 1; i < *power; i++) { - *base *= baseOriginal; +unsigned long Calculate(unsigned long base, unsigned long power) { + unsigned long baseOriginal = base; + if (power == 0) return 1; // power zero + if (power > 0) { // power positive + for (unsigned int i = 1; i < power; i++) { + base *= baseOriginal; } } + return base; } diff --git a/8_Power_Calculator/8_Power_Calculator.h b/8_Power_Calculator/8_Power_Calculator.h index 3013847..a898cd2 100644 --- a/8_Power_Calculator/8_Power_Calculator.h +++ b/8_Power_Calculator/8_Power_Calculator.h @@ -2,4 +2,4 @@ #include //functions -void Calculate(unsigned long*, unsigned long*); +unsigned long Calculate(unsigned long, unsigned long); -- 2.52.0 From 662edb7b2e0b87e07004fe350bde2aa876f5b221 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 6 Jun 2023 08:25:30 +0200 Subject: [PATCH 3/4] task 9 --- 9_Num_Sort/9_Num_Sort.c | 30 ++++++++++++++++++++++++++++++ 9_Num_Sort/9_Num_Sort.h | 3 +++ 2 files changed, 33 insertions(+) create mode 100644 9_Num_Sort/9_Num_Sort.c create mode 100644 9_Num_Sort/9_Num_Sort.h diff --git a/9_Num_Sort/9_Num_Sort.c b/9_Num_Sort/9_Num_Sort.c new file mode 100644 index 0000000..02bbeab --- /dev/null +++ b/9_Num_Sort/9_Num_Sort.c @@ -0,0 +1,30 @@ +// Bubblesort + +#include "9_Num_Sort.h" + +int main() { + // vars + int nums[MAXARR] = {12,15,17,6,2,4,7,6,3,10}; + + // sort + for (int max = sizeof(nums)/4 - 1; max >= 0; max--) { + //vars + int temp = 0; + + // main loop + for (int i = 0; i < sizeof(nums)/4 - 1; i++) { // sizeof -> bytes, so divided by type size; -1 to avoid overflow + if (nums[i] > nums[i + 1]) { + temp = nums[i]; // switching the numbers + nums[i] = nums[i + 1]; + nums[i + 1] = temp; + } + } + } + + // output + for (int i = 0; i < sizeof(nums)/4; i++) { // sizeof -> bytes, so divided by type size; -1 to avoid overflow + printf("%i\n", nums[i]); + } + + return 0; +} diff --git a/9_Num_Sort/9_Num_Sort.h b/9_Num_Sort/9_Num_Sort.h new file mode 100644 index 0000000..7acb2f5 --- /dev/null +++ b/9_Num_Sort/9_Num_Sort.h @@ -0,0 +1,3 @@ +#include + +#define MAXARR 10 -- 2.52.0 From eb1bade54ab97180173b07aa3da59eab64ea9b58 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Tue, 6 Jun 2023 10:29:16 +0200 Subject: [PATCH 4/4] Lottery: 6 of 49 --- 10_Lotto/10_Lotto.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ 10_Lotto/10_Lotto.h | 16 +++++++ 2 files changed, 116 insertions(+) create mode 100644 10_Lotto/10_Lotto.c create mode 100644 10_Lotto/10_Lotto.h diff --git a/10_Lotto/10_Lotto.c b/10_Lotto/10_Lotto.c new file mode 100644 index 0000000..5c44e64 --- /dev/null +++ b/10_Lotto/10_Lotto.c @@ -0,0 +1,100 @@ +#include "10_Lotto.h" + +int main() { + // vars + int usr_input[ARRLENGTH] = {0}, ran_nums[ARRLENGTH] = {0}, amnt_correct; + + // input + for (int i = 0; i < ARRLENGTH; i++) { + usr_input[i] = Input(usr_input); + } + + // initializing number generator + Initialize(); + + // fill lottery array + RanArray_Fill(ran_nums); + + // check for correct guesses and writes the num of correct guesses in amnt_correct + amnt_correct = CheckGuess(usr_input, ran_nums); + + // output + Output(amnt_correct); + + // debugging + for (int i = 0; i < ARRLENGTH; i++) // output user input + { + printf("\nusr_in %i: %i", i, usr_input[i]); + } + + for (int i = 0; i < ARRLENGTH; i++) // output random numbers + { + printf("\nran_num %i: %i", i, ran_nums[i]); + } + + + return 0; +} + +void Initialize() { + time_t t; + srand(time(&t)); +} + +int Input(int usr_input[]) { + int input; + while (1) { + printf("\nPlease type in a number: "); + scanf("%i", &input); // user input number + if (CheckNotInNums(usr_input, input) && ValidNum(input)) { + break; // returns num if accectable + } + printf("\nNumber is not allowed."); // error: invalid number + } + return input; +} + +void Output(int amnt_correct) { + printf("\nYou got %i numbers right!", amnt_correct); +} + +int CheckGuess(int usr_in[], int ran_nums[]) { + int count = 0; + for (int i = 0; i < ARRLENGTH; i++) // iterating through usr_in + { + if (CheckNotInNums(ran_nums, usr_in[i]) == 0) { // checks and counts how often each num of usr_in is in ran_nums + count++; + continue; + } + } + return count; // returns amount of right guesses +} + +void RanArray_Fill(int arr[]) { + int random_number; + for (int i = 0; i < ARRLENGTH; i++) { // fill array with random values from 1 to 49 + while (1) { + random_number = RanNum(); + if (CheckNotInNums(arr, random_number)) { // check if random number doesn't exist yet + arr[i] = random_number; // if check successfull: writes random number in array + break; + } + } + } +} + +int RanNum() { + return (rand() % 49) + 1; // returns random values from 1 to 49 +} + +int CheckNotInNums(int arr[], int num) { + for (int i = 0; i < ARRLENGTH; i++) { + if (arr[i] == num) return 0; // returns 0 if num exists already + } + return 1; // returns 1 if num doesn't exist yet +} + +int ValidNum(int input) { + if (input > 0 && input < 50) return 1; // returns 1 if input is between 0 and 50, thus valid + return 0; // returns 0 if input is NOT between 0 and 50, thus invalid +} diff --git a/10_Lotto/10_Lotto.h b/10_Lotto/10_Lotto.h new file mode 100644 index 0000000..c05f104 --- /dev/null +++ b/10_Lotto/10_Lotto.h @@ -0,0 +1,16 @@ +// includes +#include +#include + +// defines +#define ARRLENGTH 6 + +// function declarations +void Initialize(); +int Input(int[]); +void Output(int); +int CheckGuess(int[], int[]); +void RanArray_Fill(int[]); +int RanNum(); +int CheckNotInNums(int[], int); +int ValidNum(int); -- 2.52.0