From 12a2b6e486c8ba49edc9d42d1e7273593261604d Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Fri, 29 Sep 2023 16:20:43 +0300 Subject: [PATCH 01/23] add fibonacci.c --- semester_1/homework2/fibonacci.c | 198 ++++++++++++++++++++++--------- 1 file changed, 145 insertions(+), 53 deletions(-) diff --git a/semester_1/homework2/fibonacci.c b/semester_1/homework2/fibonacci.c index edd0188..73af436 100644 --- a/semester_1/homework2/fibonacci.c +++ b/semester_1/homework2/fibonacci.c @@ -1,91 +1,183 @@ #include #include -#include -bool checkIfNumberIsCorrect(int n) +int iterativeFibonacci(int number, long long int *answer); +int recursiveFibonacci(int number, long long int *answer); +long long int recursiveFibonacciBody(int number); +bool correctInputCheckForRecursiveFunction(int number); +bool testIterativeFunction(void); +bool testRecursiveFunction(void); +bool test(void); + +int main() { - if (n < 1) + + if (!test()) { - return false; + printf("\nSorry but the program does not work correctly\n"); + return 1; } - return true; + + printf("\nEnter the number of fbonacci number you want to see: "); + int number = 0; + int scanfNumberOfElements = scanf("%d", &number); + + if (scanfNumberOfElements != 1) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + long long int iterativeAnswer = 0; + int errorCode1 = iterativeFibonacci(number, &iterativeAnswer); + if (errorCode1 != 0) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + long long int recursiveAnswer = 0; + int errorCode2 = recursiveFibonacci(number, &recursiveAnswer); + if (errorCode2 != 0) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + if (iterativeAnswer != recursiveAnswer) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + printf("\nResult of work of iterative function: %lld", iterativeAnswer); + printf("\nResult of work of recursive function: %lld\n", recursiveAnswer); + + return 0; } -int iterativeFibonacci(int number) +int iterativeFibonacci(int number, long long int *answer) { - - if (number <= 3) + if (number < 1) { - int firstThreeNumbers[3] = {1, 1, 2}; - return firstThreeNumbers[number - 1]; + *answer = -1; + return 1; } - int previousNumber = 1; - int previousNumberForPreviousNumber = 1; - int currentNumber = 1; - int buffer = 0; + long long int currentNumbers[3] = {1, 1, 2}; + + if (number < 4) + { + *answer = currentNumbers[number - 1]; + return 0; + } - for (int i = 2; i < number; i++) + for (int n = 2; n <= number; ++n) { - previousNumberForPreviousNumber = previousNumber; - previousNumber = currentNumber; - currentNumber = previousNumber + previousNumberForPreviousNumber; + currentNumbers[2] = currentNumbers[1] + currentNumbers[0]; + currentNumbers[0] = currentNumbers[1]; + currentNumbers[1] = currentNumbers[2]; } - return currentNumber; + *answer = currentNumbers[0]; + return 0; } -int recursiveFibonacci(int number) +int recursiveFibonacci(int number, long long int *answer) { + if (!correctInputCheckForRecursiveFunction(number)) + { + return 1; + } + + *answer = recursiveFibonacciBody(number); + return 0; +} + +long long int recursiveFibonacciBody(int number) +{ if (number == 0) { return 0; } - if (number == 1) { return 1; } + return recursiveFibonacciBody(number - 1) + recursiveFibonacciBody(number - 2); +} - return recursiveFibonacci(number - 2) + recursiveFibonacci(number - 1); +bool correctInputCheckForRecursiveFunction(int number) +{ + if (number < 1) + { + return false; + } + return true; } -int timeComparison() +bool testIterativeFunction(void) { - float recursiveFunctionTimeOfWork = 1; - float iterativeFunctionTimeOfWork = 1; - int i = 1; - while (recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork < 2.0) - { - - float startTimeOfWorkIterative, finishTimeOfWorkIterative; - startTimeOfWorkIterative = clock(); - int iterativeFunctionWorkResult = iterativeFibonacci(i); - finishTimeOfWorkIterative = clock(); - iterativeFunctionTimeOfWork = finishTimeOfWorkIterative - startTimeOfWorkIterative; - - float startTimeOfWorkRecursive, finishTimeOfWorkRecursive; - startTimeOfWorkRecursive = clock(); - int recursiveFunctionWorkResult = recursiveFibonacci(i); - finishTimeOfWorkRecursive = clock(); - recursiveFunctionTimeOfWork = finishTimeOfWorkRecursive - startTimeOfWorkRecursive; - - if (iterativeFunctionWorkResult != recursiveFunctionWorkResult) - { - printf("Different answers for same arguments"); - return 0; - } - printf("%f", recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork); - - } - return i; + long long int test1 = 0; + int errorCode1 = iterativeFibonacci(-5, &test1); + if (errorCode1 != 1) + { + return false; + } + + long long int test2 = 0; + int errorCode2 = iterativeFibonacci(3, &test2); + if (test2 != 2 || errorCode2 != 0) + { + return false; + } + + long long int test3 = 0; + int errorCode3 = iterativeFibonacci(21, &test3); + if (test3 != 10946 || errorCode3 != 0) + { + return false; + } + return true; } -int main() +bool testRecursiveFunction(void) { + long long int test1 = 0; + int errorCode1 = recursiveFibonacci(-5, &test1); + if (errorCode1 != 1) + { + return false; + } - printf("%d", timeComparison()); + long long int test2 = 0; + int errorCode2 = recursiveFibonacci(3, &test2); + if (test2 != 2 || errorCode2 != 0) + { + return false; + } - return 0; + long long int test3 = 0; + int errorCode3 = recursiveFibonacci(21, &test3); + if (test3 != 10946 || errorCode3 != 0) + { + return false; + } + return true; } + +bool test(void) +{ + if (!testIterativeFunction()) + { + return false; + } + + if (!testRecursiveFunction()) + { + return false; + } + + return true; +} \ No newline at end of file From 6026da5cc1293481b4e12589c18be628ec157be6 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Fri, 29 Sep 2023 16:34:06 +0300 Subject: [PATCH 02/23] add sortings.c --- semester_1/homework2/sortings.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 semester_1/homework2/sortings.c diff --git a/semester_1/homework2/sortings.c b/semester_1/homework2/sortings.c new file mode 100644 index 0000000..e69de29 From 5bcddbe51d7026a7a91e17be880fa41033dac604 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Fri, 29 Sep 2023 22:35:08 +0300 Subject: [PATCH 03/23] add sortings file --- semester_1/homework2/sortings.c | 301 ++++++++++++++++++++++++++++++++ 1 file changed, 301 insertions(+) diff --git a/semester_1/homework2/sortings.c b/semester_1/homework2/sortings.c index e69de29..b353f34 100644 --- a/semester_1/homework2/sortings.c +++ b/semester_1/homework2/sortings.c @@ -0,0 +1,301 @@ +#include +#include +#include + +int bubbleSort(int array[], int size); +int countingSort(int array[], int size); +bool test(void); +bool checkIfArrayIsSorted(int array[], int size); +bool testBubbleSort(void); +bool testCountingSort(void); +void printArray(int array[], int size); + +int main() +{ + if (!test()) + { + printf("\nSorry but the program does not work correctly\n"); + return 1; + } + + int size = 0; + printf("Enter the size of the array: "); + scanf("%d", &size); + + while (size <= 0) + { + printf("\nSize of your array should be at least 1\n"); + printf("Enter the size of your array: "); + scanf("%d", &size); + } + + int *countingSortArray = malloc(size * sizeof(int)); + if (countingSortArray == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + int *bubbleSortArray = malloc(size * sizeof(int)); + if (bubbleSortArray == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + int currentElement = 0; + for (int i = 0; i < size; ++i) + { + printf("Enter the element number %d: ", i + 1); + scanf("%d", ¤tElement); + countingSortArray[i] = currentElement; + bubbleSortArray[i] = currentElement; + } + + int errorCode = countingSort(countingSortArray, size); + + if (errorCode == 1) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + errorCode = bubbleSort(bubbleSortArray, size); + + if (errorCode == 1) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + printf("\nResult of work of counting sort: \n"); + printArray(countingSortArray, size); + + printf("\nResult of work of bubble sort: \n"); + printArray(bubbleSortArray, size); + + printf("\n"); + + return 0; +} + +int bubbleSort(int array[], int size) +{ + if (size < 0) + { + return 2; + } + + if (size == 0) + { + return 0; + } + + int buffer; + int i = 0; + bool alreadySwapped = false; + + do + { + alreadySwapped = false; + + for (int j = 0; j < size - 1 - i; ++j) + { + if (array[j] > array[j + 1]) + { + buffer = array[j]; + array[j] = array[j + 1]; + array[j + 1] = buffer; + alreadySwapped = true; + } + } + + ++i; + + } while (alreadySwapped); + + return 0; +} + +void printArray(int array[], int size) +{ + for (int i = 0; i < size; i++) + { + printf("%d ", array[i]); + } + printf("\n"); +} + +int countingSort(int array[], int size) +{ + if (size < 0) + { + return 2; + } + + if (size == 0) + { + return 0; + } + + int maxElement = array[0]; + for (int i = 0; i < size; ++i) + { + if (array[i] > maxElement) + { + maxElement = array[i]; + } + } + + int *countArray = malloc(maxElement * sizeof(int)); + if (countArray == NULL) + { + return 1; + } + + for (int i = 0; i < size; ++i) + { + ++countArray[array[i]]; + } + + for (int i = 1; i < maxElement; ++i) + { + countArray[i] += countArray[i - 1]; + } + + for (int i = maxElement; i > 0; --i) + { + countArray[i] = countArray[i - 1]; + } + countArray[0] = 0; + + int *outputArray = malloc(size * sizeof(int)); + if (outputArray == NULL) + { + free(countArray); + return 1; + } + + for (int i = 0; i < size; ++i) + { + outputArray[countArray[array[i]]] = array[i]; + ++countArray[array[i]]; + } + + for (int i = 0; i < size; ++i) + { + array[i] = outputArray[i]; + } + + free(countArray); + free(outputArray); + return 0; +} + +bool checkIfArrayIsSorted(int array[], int size) +{ + int previousElement = array[0]; + for (int i = 1; i < size; ++i) + { + if (previousElement > array[i]) + { + return false; + } + } + return true; +} + +bool test() +{ + if (!testBubbleSort()) + { + return false; + } + + if (!testCountingSort()) + { + return false; + } + + return true; +} + +bool testBubbleSort(void) +{ + int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int errorCode1 = bubbleSort(testArray1, 10); + if (errorCode1 != 0 || !checkIfArrayIsSorted(testArray1, 10)) + { + return false; + } + + int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + int errorCode2 = bubbleSort(testArray2, 10); + if (errorCode2 != 0 || !checkIfArrayIsSorted(testArray2, 10)) + { + return false; + } + + int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; + int errorCode3 = bubbleSort(testArray3, 10); + if (errorCode3 != 0 || !checkIfArrayIsSorted(testArray3, 10)) + { + return false; + } + + int testArray4[10] = {0}; + int errorCode4 = bubbleSort(testArray4, 10); + if (errorCode4 != 0 || !checkIfArrayIsSorted(testArray4, 10)) + { + return false; + } + + int testArray5[1] = {1}; + int errorCode5 = bubbleSort(testArray5, -1); + if (errorCode5 != 2) + { + return false; + } + + return true; +} + +bool testCountingSort(void) +{ + int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int errorCode1 = countingSort(testArray1, 10); + if (errorCode1 != 0 || !checkIfArrayIsSorted(testArray1, 10)) + { + return false; + } + + int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + int errorCode2 = countingSort(testArray2, 10); + if (errorCode2 != 0 || !checkIfArrayIsSorted(testArray2, 10)) + { + return false; + } + + int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; + int errorCode3 = countingSort(testArray3, 10); + if (errorCode3 != 0 || !checkIfArrayIsSorted(testArray3, 10)) + { + return false; + } + + int testArray4[10] = {0}; + int errorCode4 = countingSort(testArray4, 10); + if (errorCode4 != 0 || !checkIfArrayIsSorted(testArray4, 10)) + { + return false; + } + + int testArray5[1] = {1}; + int errorCode5 = countingSort(testArray5, -1); + if (errorCode5 != 2) + { + return false; + } + + return true; +} From 6abef45a38d42613b55076d8c7c58de37161e183 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Sat, 30 Sep 2023 11:19:58 +0300 Subject: [PATCH 04/23] 1 --- semester_1/homework1/substring.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/semester_1/homework1/substring.c b/semester_1/homework1/substring.c index 35fff2a..412e38f 100644 --- a/semester_1/homework1/substring.c +++ b/semester_1/homework1/substring.c @@ -1,7 +1,7 @@ #include #include -int countingSubstrings(char *string, char *subString, int lengthOfString, int lengthOfSubstring); +int countSubstrings(const char *string, const char *subString, int lengthOfString, int lengthOfSubstring); int readingLineFromConsole(char *string); bool test(void); @@ -13,18 +13,18 @@ int main() return 1; } printf("Enter your string\n"); - char S[1000]; - int lengthOfS = readingLineFromConsole(S); + char s[1000] = { '\0' }; + int lengthOfS = readingLineFromConsole(s); printf("Enter your substring\n"); - char S1[1000]; - int lengthOfS1 = readingLineFromConsole(S1); - int numberOfSubstrings = countingSubstrings(S, S1, lengthOfS, lengthOfS1); + char s1[1000] = { '\0' }; + int lengthOfS1 = readingLineFromConsole(s1); + int numberOfSubstrings = countSubstrings(s, s1, lengthOfS, lengthOfS1); printf("The number of occurances of your substring in your string is %d\n", numberOfSubstrings); return 0; } -int countingSubstrings(char *string, char *subString, int lengthOfString, int lengthOfSubstring) +int countSubstrings(const char *string, const char *subString, int lengthOfString, int lengthOfSubstring) { int count = 0; bool doWeCompareRightNow = false; @@ -35,9 +35,10 @@ int countingSubstrings(char *string, char *subString, int lengthOfString, int le { if (doWeCompareRightNow) { - if (++currentSubstringCharacter < lengthOfSubstring) + ++currentSubstringCharacter; + if (currentSubstringCharacter < lengthOfSubstring) { - doWeCompareRightNow = (string[currentCharacter] == subString[currentSubstringCharacter] ? true : false); + doWeCompareRightNow = string[currentCharacter] == subString[currentSubstringCharacter]; } if (!doWeCompareRightNow) @@ -65,7 +66,7 @@ int countingSubstrings(char *string, char *subString, int lengthOfString, int le int readingLineFromConsole(char *string) { int c = 0; - char ch; + char ch = "\0"; do { ch = getchar(); @@ -86,7 +87,7 @@ bool test(void) int testStringLength1 = 0; int testSubStringLength1 = 6; int answer1 = 0; - if (!(countingSubstrings(testString1, testSubstring1, testStringLength1, testSubStringLength1) == answer1)) + if (!(countSubstrings(testString1, testSubstring1, testStringLength1, testSubStringLength1) == answer1)) { printf("%d", 1); return false; @@ -97,7 +98,7 @@ bool test(void) int testStringLength2 = 23; int testSubStringLength2 = 3; int answer2 = 4; - if (!(countingSubstrings(testString2, testSubstring2, testStringLength2, testSubStringLength2) == answer2)) + if (!(countSubstrings(testString2, testSubstring2, testStringLength2, testSubStringLength2) == answer2)) { return false; } @@ -107,7 +108,7 @@ bool test(void) int testStringLength3 = 9; int testSubStringLength3 = 0; int answer3 = 0; - if (!(countingSubstrings(testString3, testSubstring3, testStringLength3, testSubStringLength3) == answer3)) + if (!(countSubstrings(testString3, testSubstring3, testStringLength3, testSubStringLength3) == answer3)) { return false; } @@ -116,7 +117,7 @@ bool test(void) int testStringLength4 = 5; int testSubStringLength4 = 1; int answer4 = 5; - if (!(countingSubstrings(testString4, testSubstring4, testStringLength4, testSubStringLength4) == answer4)) + if (!(countSubstrings(testString4, testSubstring4, testStringLength4, testSubStringLength4) == answer4)) { return false; } @@ -126,7 +127,7 @@ bool test(void) int testStringLength5 = 2; int testSubStringLength5 = 2; int answer5 = 1; - if (!(countingSubstrings(testString5, testSubstring5, testStringLength5, testSubStringLength5) == answer5)) + if (!(countSubstrings(testString5, testSubstring5, testStringLength5, testSubStringLength5) == answer5)) { return false; } From f2d183852992128d7312b197826b7ff4cdfb6b89 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 12:21:04 +0300 Subject: [PATCH 05/23] create the file --- semester_1/homework2/search.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 semester_1/homework2/search.c diff --git a/semester_1/homework2/search.c b/semester_1/homework2/search.c new file mode 100644 index 0000000..e69de29 From 0900edc99a653bc7ce94ed3eca6225eb3af0b754 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 12:35:41 +0300 Subject: [PATCH 06/23] - --- semester_1/homework2/search.c | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/semester_1/homework2/search.c b/semester_1/homework2/search.c index e69de29..c4d6a69 100644 --- a/semester_1/homework2/search.c +++ b/semester_1/homework2/search.c @@ -0,0 +1,64 @@ +#include + +void generateRandomArray(int array[], int size); +void printArray(int array[], int size); + +int main() +{ + srand(time(0)); + + printf("\nEnter the size of your array: "); + int size = 0; + scanf("%d", &size); + + while (size <= 0) + { + printf("\nSize of your array should be at least 1\n"); + printf("Enter the size of your array: "); + scanf("%d", &size); + } + + int *array = malloc(size * sizeof(int)); + if (array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + generateRandomArray(array, size); + + printf("\nEnter the quantity of numbers for tests: "); + int k = 0; + scanf("%d", &k); + + int *k_array = malloc(k * sizeof(int)); + if (k_array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + generateRandomArray(k_array, k); + + printArray(array, size); + printArray(k_array, k); + return 0; +} + +void generateRandomArray(int array[], int size) +{ + for (int i = 0; i < size; ++i) + { + array[i] = rand() % 100; + } +} + +void printArray(int array[], int size) +{ + for (int i = 0; i < size; i++) + { + printf("%d ", array[i]); + } + printf("\n"); +} + From d2aab5ec1f0a5b7a8d64ac6a113658b571112254 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 12:36:31 +0300 Subject: [PATCH 07/23] - --- semester_1/homework2/sortings.c | 301 -------------------------------- 1 file changed, 301 deletions(-) delete mode 100644 semester_1/homework2/sortings.c diff --git a/semester_1/homework2/sortings.c b/semester_1/homework2/sortings.c deleted file mode 100644 index b353f34..0000000 --- a/semester_1/homework2/sortings.c +++ /dev/null @@ -1,301 +0,0 @@ -#include -#include -#include - -int bubbleSort(int array[], int size); -int countingSort(int array[], int size); -bool test(void); -bool checkIfArrayIsSorted(int array[], int size); -bool testBubbleSort(void); -bool testCountingSort(void); -void printArray(int array[], int size); - -int main() -{ - if (!test()) - { - printf("\nSorry but the program does not work correctly\n"); - return 1; - } - - int size = 0; - printf("Enter the size of the array: "); - scanf("%d", &size); - - while (size <= 0) - { - printf("\nSize of your array should be at least 1\n"); - printf("Enter the size of your array: "); - scanf("%d", &size); - } - - int *countingSortArray = malloc(size * sizeof(int)); - if (countingSortArray == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - int *bubbleSortArray = malloc(size * sizeof(int)); - if (bubbleSortArray == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - int currentElement = 0; - for (int i = 0; i < size; ++i) - { - printf("Enter the element number %d: ", i + 1); - scanf("%d", ¤tElement); - countingSortArray[i] = currentElement; - bubbleSortArray[i] = currentElement; - } - - int errorCode = countingSort(countingSortArray, size); - - if (errorCode == 1) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - errorCode = bubbleSort(bubbleSortArray, size); - - if (errorCode == 1) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - printf("\nResult of work of counting sort: \n"); - printArray(countingSortArray, size); - - printf("\nResult of work of bubble sort: \n"); - printArray(bubbleSortArray, size); - - printf("\n"); - - return 0; -} - -int bubbleSort(int array[], int size) -{ - if (size < 0) - { - return 2; - } - - if (size == 0) - { - return 0; - } - - int buffer; - int i = 0; - bool alreadySwapped = false; - - do - { - alreadySwapped = false; - - for (int j = 0; j < size - 1 - i; ++j) - { - if (array[j] > array[j + 1]) - { - buffer = array[j]; - array[j] = array[j + 1]; - array[j + 1] = buffer; - alreadySwapped = true; - } - } - - ++i; - - } while (alreadySwapped); - - return 0; -} - -void printArray(int array[], int size) -{ - for (int i = 0; i < size; i++) - { - printf("%d ", array[i]); - } - printf("\n"); -} - -int countingSort(int array[], int size) -{ - if (size < 0) - { - return 2; - } - - if (size == 0) - { - return 0; - } - - int maxElement = array[0]; - for (int i = 0; i < size; ++i) - { - if (array[i] > maxElement) - { - maxElement = array[i]; - } - } - - int *countArray = malloc(maxElement * sizeof(int)); - if (countArray == NULL) - { - return 1; - } - - for (int i = 0; i < size; ++i) - { - ++countArray[array[i]]; - } - - for (int i = 1; i < maxElement; ++i) - { - countArray[i] += countArray[i - 1]; - } - - for (int i = maxElement; i > 0; --i) - { - countArray[i] = countArray[i - 1]; - } - countArray[0] = 0; - - int *outputArray = malloc(size * sizeof(int)); - if (outputArray == NULL) - { - free(countArray); - return 1; - } - - for (int i = 0; i < size; ++i) - { - outputArray[countArray[array[i]]] = array[i]; - ++countArray[array[i]]; - } - - for (int i = 0; i < size; ++i) - { - array[i] = outputArray[i]; - } - - free(countArray); - free(outputArray); - return 0; -} - -bool checkIfArrayIsSorted(int array[], int size) -{ - int previousElement = array[0]; - for (int i = 1; i < size; ++i) - { - if (previousElement > array[i]) - { - return false; - } - } - return true; -} - -bool test() -{ - if (!testBubbleSort()) - { - return false; - } - - if (!testCountingSort()) - { - return false; - } - - return true; -} - -bool testBubbleSort(void) -{ - int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int errorCode1 = bubbleSort(testArray1, 10); - if (errorCode1 != 0 || !checkIfArrayIsSorted(testArray1, 10)) - { - return false; - } - - int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; - int errorCode2 = bubbleSort(testArray2, 10); - if (errorCode2 != 0 || !checkIfArrayIsSorted(testArray2, 10)) - { - return false; - } - - int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; - int errorCode3 = bubbleSort(testArray3, 10); - if (errorCode3 != 0 || !checkIfArrayIsSorted(testArray3, 10)) - { - return false; - } - - int testArray4[10] = {0}; - int errorCode4 = bubbleSort(testArray4, 10); - if (errorCode4 != 0 || !checkIfArrayIsSorted(testArray4, 10)) - { - return false; - } - - int testArray5[1] = {1}; - int errorCode5 = bubbleSort(testArray5, -1); - if (errorCode5 != 2) - { - return false; - } - - return true; -} - -bool testCountingSort(void) -{ - int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int errorCode1 = countingSort(testArray1, 10); - if (errorCode1 != 0 || !checkIfArrayIsSorted(testArray1, 10)) - { - return false; - } - - int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; - int errorCode2 = countingSort(testArray2, 10); - if (errorCode2 != 0 || !checkIfArrayIsSorted(testArray2, 10)) - { - return false; - } - - int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; - int errorCode3 = countingSort(testArray3, 10); - if (errorCode3 != 0 || !checkIfArrayIsSorted(testArray3, 10)) - { - return false; - } - - int testArray4[10] = {0}; - int errorCode4 = countingSort(testArray4, 10); - if (errorCode4 != 0 || !checkIfArrayIsSorted(testArray4, 10)) - { - return false; - } - - int testArray5[1] = {1}; - int errorCode5 = countingSort(testArray5, -1); - if (errorCode5 != 2) - { - return false; - } - - return true; -} From 69e13dd86dca4d952f47d708f682abaf792cfcc7 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 12:48:20 +0300 Subject: [PATCH 08/23] - --- semester_1/homework2/search.c | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/semester_1/homework2/search.c b/semester_1/homework2/search.c index c4d6a69..f776681 100644 --- a/semester_1/homework2/search.c +++ b/semester_1/homework2/search.c @@ -1,4 +1,7 @@ #include +#include +#include +#include void generateRandomArray(int array[], int size); void printArray(int array[], int size); @@ -40,8 +43,18 @@ int main() generateRandomArray(k_array, k); + + printArray(array, size); printArray(k_array, k); + + + for (int i = 0; i < k; ++i) + { + + } + + return 0; } @@ -55,10 +68,35 @@ void generateRandomArray(int array[], int size) void printArray(int array[], int size) { - for (int i = 0; i < size; i++) + for (int i = 0; i < size; ++i) { printf("%d ", array[i]); } printf("\n"); } +int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) +{ + while (leftEdge <= rightEdge) + { + int middleElement = leftEdge + (rightEdge - leftEdge) / 2; + + if (array[middleElement] == x) + { + *answer = middleElement; + return 0; + } + + if (array[middleElement] < x) + { + leftEdge = middleElement + 1; + } + + else + { + rightEdge = middleElement - 1; + } + } + + return -1; +} From 553c768d14ee42c6082525ada673760c01e688da Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 14:58:29 +0300 Subject: [PATCH 09/23] change directory --- semester_1/homework3/search.c | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 semester_1/homework3/search.c diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c new file mode 100644 index 0000000..f776681 --- /dev/null +++ b/semester_1/homework3/search.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +void generateRandomArray(int array[], int size); +void printArray(int array[], int size); + +int main() +{ + srand(time(0)); + + printf("\nEnter the size of your array: "); + int size = 0; + scanf("%d", &size); + + while (size <= 0) + { + printf("\nSize of your array should be at least 1\n"); + printf("Enter the size of your array: "); + scanf("%d", &size); + } + + int *array = malloc(size * sizeof(int)); + if (array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + generateRandomArray(array, size); + + printf("\nEnter the quantity of numbers for tests: "); + int k = 0; + scanf("%d", &k); + + int *k_array = malloc(k * sizeof(int)); + if (k_array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + generateRandomArray(k_array, k); + + + + printArray(array, size); + printArray(k_array, k); + + + for (int i = 0; i < k; ++i) + { + + } + + + return 0; +} + +void generateRandomArray(int array[], int size) +{ + for (int i = 0; i < size; ++i) + { + array[i] = rand() % 100; + } +} + +void printArray(int array[], int size) +{ + for (int i = 0; i < size; ++i) + { + printf("%d ", array[i]); + } + printf("\n"); +} + +int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) +{ + while (leftEdge <= rightEdge) + { + int middleElement = leftEdge + (rightEdge - leftEdge) / 2; + + if (array[middleElement] == x) + { + *answer = middleElement; + return 0; + } + + if (array[middleElement] < x) + { + leftEdge = middleElement + 1; + } + + else + { + rightEdge = middleElement - 1; + } + } + + return -1; +} From 5bcf83a74f7fabe10a393da7fc634a7bfa9e8e43 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 15:40:09 +0300 Subject: [PATCH 10/23] tests1 --- semester_1/homework3/search.c | 61 ++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index f776681..6560e7c 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -5,6 +5,8 @@ void generateRandomArray(int array[], int size); void printArray(int array[], int size); +int binarySearch(int array[], int x, int leftEdge, int rightEdge); +bool testBinarySearchFunction(void); int main() { @@ -43,18 +45,22 @@ int main() generateRandomArray(k_array, k); - - printArray(array, size); printArray(k_array, k); - for (int i = 0; i < k; ++i) { - + int number = k_array[i]; + if (binarySearch(array, number, 0, size - 1) != -1) + { + printf("%d is in array\n", k_array[i]); + } + else + { + printf("%d is not in array\n", k_array[i]); + } } - return 0; } @@ -75,7 +81,7 @@ void printArray(int array[], int size) printf("\n"); } -int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) +int binarySearch(int array[], int x, int leftEdge, int rightEdge) { while (leftEdge <= rightEdge) { @@ -83,8 +89,7 @@ int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) if (array[middleElement] == x) { - *answer = middleElement; - return 0; + return middleElement; } if (array[middleElement] < x) @@ -100,3 +105,43 @@ int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) return -1; } + +bool testBinarySearchFunction(void) +{ + int testArray[10] = {}; + int test = ; + if (binarySearch(testArray, test, 0, 9) != ) + { + return false; + } + + int testArray[10] = {}; + int test = ; + if (binarySearch(testArray, test, 0, 9) != ) + { + return false; + } + + int testArray[10] = {}; + int test = ; + if (binarySearch(testArray, test, 0, 9) != ) + { + return false; + } + + int testArray[10] = {}; + int test = ; + if (binarySearch(testArray, test, 0, 9) != ) + { + return false; + } + + int testArray[10] = {}; + int test = ; + if (binarySearch(testArray, test, 0, 9) != ) + { + return false; + } + + return true; +} \ No newline at end of file From a9c59881c1b05f6211f54113e669b9324c6643af Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 15:41:28 +0300 Subject: [PATCH 11/23] - --- semester_1/homework2/search.c | 102 ---------------------------------- 1 file changed, 102 deletions(-) delete mode 100644 semester_1/homework2/search.c diff --git a/semester_1/homework2/search.c b/semester_1/homework2/search.c deleted file mode 100644 index f776681..0000000 --- a/semester_1/homework2/search.c +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -#include -#include - -void generateRandomArray(int array[], int size); -void printArray(int array[], int size); - -int main() -{ - srand(time(0)); - - printf("\nEnter the size of your array: "); - int size = 0; - scanf("%d", &size); - - while (size <= 0) - { - printf("\nSize of your array should be at least 1\n"); - printf("Enter the size of your array: "); - scanf("%d", &size); - } - - int *array = malloc(size * sizeof(int)); - if (array == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - generateRandomArray(array, size); - - printf("\nEnter the quantity of numbers for tests: "); - int k = 0; - scanf("%d", &k); - - int *k_array = malloc(k * sizeof(int)); - if (k_array == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - generateRandomArray(k_array, k); - - - - printArray(array, size); - printArray(k_array, k); - - - for (int i = 0; i < k; ++i) - { - - } - - - return 0; -} - -void generateRandomArray(int array[], int size) -{ - for (int i = 0; i < size; ++i) - { - array[i] = rand() % 100; - } -} - -void printArray(int array[], int size) -{ - for (int i = 0; i < size; ++i) - { - printf("%d ", array[i]); - } - printf("\n"); -} - -int binarySearch(int array[], int x, int leftEdge, int rightEdge, int *answer) -{ - while (leftEdge <= rightEdge) - { - int middleElement = leftEdge + (rightEdge - leftEdge) / 2; - - if (array[middleElement] == x) - { - *answer = middleElement; - return 0; - } - - if (array[middleElement] < x) - { - leftEdge = middleElement + 1; - } - - else - { - rightEdge = middleElement - 1; - } - } - - return -1; -} From 72f341cb57f359e1a419d7ad52c2b129a826a1a2 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 2 Oct 2023 15:46:20 +0300 Subject: [PATCH 12/23] - --- semester_1/homework3/search.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index 6560e7c..c26e058 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -108,21 +108,21 @@ int binarySearch(int array[], int x, int leftEdge, int rightEdge) bool testBinarySearchFunction(void) { - int testArray[10] = {}; + int testArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int test = ; if (binarySearch(testArray, test, 0, 9) != ) { return false; } - int testArray[10] = {}; + int testArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int test = ; if (binarySearch(testArray, test, 0, 9) != ) { return false; } - int testArray[10] = {}; + int testArray[10] = {9, 8, 4, 6, 2, 4, 2, 8, 5, 4}; int test = ; if (binarySearch(testArray, test, 0, 9) != ) { From d6b2f9dac68e77852fb93a57263d04be710a984d Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Tue, 3 Oct 2023 17:04:37 +0300 Subject: [PATCH 13/23] - --- semester_1/homework3/search.c | 122 ++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 41 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index c26e058..21e7f20 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -3,10 +3,18 @@ #include #include -void generateRandomArray(int array[], int size); +int partition(int array[], int leftElement, int rightElement); +void swap(int *firstValue, int *secondValue); void printArray(int array[], int size); +void generateRandomArray(int array[], int size); +void insertionSort(int array[], int leftElement, int rightElement); int binarySearch(int array[], int x, int leftEdge, int rightEdge); -bool testBinarySearchFunction(void); +void smartQuickSort(int array[], int leftElement, int rightElement); + +bool test(void); +bool testSmartQuickSearch(void); +bool testPartitionFunction(void); +bool testSwapFunction(void); int main() { @@ -35,6 +43,7 @@ int main() printf("\nEnter the quantity of numbers for tests: "); int k = 0; scanf("%d", &k); + printf("\n"); int *k_array = malloc(k * sizeof(int)); if (k_array == NULL) @@ -45,8 +54,13 @@ int main() generateRandomArray(k_array, k); + printf("Your array: "); printArray(array, size); + printf("Your numbers to search in array: "); printArray(k_array, k); + printf("\n"); + + smartQuickSort(array, 0, size - 1); for (int i = 0; i < k; ++i) { @@ -60,10 +74,62 @@ int main() printf("%d is not in array\n", k_array[i]); } } + printf("\n"); return 0; } +void printArray(int array[], int size) +{ + for (int i = 0; i < size; ++i) + { + printf("%d ", array[i]); + } + printf("\n"); +} + +void swap(int *firstValue, int *secondValue) +{ + int buffer = *firstValue; + *firstValue = *secondValue; + *secondValue = buffer; +} + +int partition(int array[], int leftElement, int rightElement) +{ + rightElement -= 1; + int currentSeparator = array[leftElement]; + int i = rightElement; + + for (int j = rightElement; j > leftElement; --j) + { + if (array[j] >= currentSeparator) + { + swap(&array[i], &array[j]); + --i; + } + } + + swap(&array[i], &array[leftElement]); + + return i; +} + +void smartQuickSort(int array[], int leftElement, int rightElement) +{ + if (rightElement - leftElement + 1 < 10) + { + insertionSort(array, leftElement, rightElement + 1); + return; + } + if (leftElement < rightElement) + { + int pivotIndex = partition(array, 0, rightElement); + smartQuickSort(array, leftElement, pivotIndex - 1); + smartQuickSort(array, pivotIndex + 1, rightElement); + } +} + void generateRandomArray(int array[], int size) { for (int i = 0; i < size; ++i) @@ -72,13 +138,19 @@ void generateRandomArray(int array[], int size) } } -void printArray(int array[], int size) +void insertionSort(int array[], int leftElement, int rightElement) { - for (int i = 0; i < size; ++i) + for (int i = leftElement; i < rightElement; i++) { - printf("%d ", array[i]); + int currentElement = array[i]; + int j = i - 1; + while (j >= leftElement && array[j] >= currentElement) + { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = currentElement; } - printf("\n"); } int binarySearch(int array[], int x, int leftEdge, int rightEdge) @@ -106,42 +178,10 @@ int binarySearch(int array[], int x, int leftEdge, int rightEdge) return -1; } -bool testBinarySearchFunction(void) +bool test(void) { - int testArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int test = ; - if (binarySearch(testArray, test, 0, 9) != ) - { - return false; - } - - int testArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int test = ; - if (binarySearch(testArray, test, 0, 9) != ) - { - return false; - } - - int testArray[10] = {9, 8, 4, 6, 2, 4, 2, 8, 5, 4}; - int test = ; - if (binarySearch(testArray, test, 0, 9) != ) - { - return false; - } + +} - int testArray[10] = {}; - int test = ; - if (binarySearch(testArray, test, 0, 9) != ) - { - return false; - } - int testArray[10] = {}; - int test = ; - if (binarySearch(testArray, test, 0, 9) != ) - { - return false; - } - return true; -} \ No newline at end of file From 122a4302c7bc1e78d7de0a8ff6ea45bb11c10b41 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Tue, 3 Oct 2023 19:53:23 +0300 Subject: [PATCH 14/23] - --- semester_1/homework3/search.c | 123 +++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 9 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index 21e7f20..da7cf38 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -7,17 +7,29 @@ int partition(int array[], int leftElement, int rightElement); void swap(int *firstValue, int *secondValue); void printArray(int array[], int size); void generateRandomArray(int array[], int size); -void insertionSort(int array[], int leftElement, int rightElement); -int binarySearch(int array[], int x, int leftEdge, int rightEdge); +void insertSort(int array[], int leftElement, int rightElement); +int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge); void smartQuickSort(int array[], int leftElement, int rightElement); bool test(void); -bool testSmartQuickSearch(void); +bool testSmartQuickSort(void); bool testPartitionFunction(void); bool testSwapFunction(void); +bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size); +bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray); +bool testBinarySearch(void); +bool testInsertSort(void); + + int main() { + if (!test()) + { + printf("\nSorry but the program does not work correctly\n"); + return 1; + } + srand(time(0)); printf("\nEnter the size of your array: "); @@ -119,7 +131,7 @@ void smartQuickSort(int array[], int leftElement, int rightElement) { if (rightElement - leftElement + 1 < 10) { - insertionSort(array, leftElement, rightElement + 1); + insertSort(array, leftElement, rightElement + 1); return; } if (leftElement < rightElement) @@ -138,7 +150,7 @@ void generateRandomArray(int array[], int size) } } -void insertionSort(int array[], int leftElement, int rightElement) +void insertSort(int array[], int leftElement, int rightElement) { for (int i = leftElement; i < rightElement; i++) { @@ -153,18 +165,18 @@ void insertionSort(int array[], int leftElement, int rightElement) } } -int binarySearch(int array[], int x, int leftEdge, int rightEdge) +int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge) { while (leftEdge <= rightEdge) { int middleElement = leftEdge + (rightEdge - leftEdge) / 2; - if (array[middleElement] == x) + if (array[middleElement] == elementWeAreLookingFor) { return middleElement; } - if (array[middleElement] < x) + if (array[middleElement] < elementWeAreLookingFor) { leftEdge = middleElement + 1; } @@ -180,8 +192,101 @@ int binarySearch(int array[], int x, int leftEdge, int rightEdge) bool test(void) { - + if (!testSwapFunction() + || !testBinarySearch() + || !testPartitionFunction()) + { + return false; + } + + return true; +} + +bool testSwapFunction(void) +{ + int firstValue = 0; + int secondValue = 1; + swap(&firstValue, &secondValue); + if (firstValue != 1 || secondValue != 0) + { + return false; + } + return true; +} + +bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray) +{ + partition(testArray, 0, sizeOfTestArray - 1); + for (int i = 0; i < 9; ++i) + { + if (testArray[i] != answerArray[i]) + { + return false; + } + } + return true; } +bool testsForPartitionFunction(void) +{ + int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; + int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; + + int testArray2[6] = {43, 36, 55, 60, 94, 24}; + int answerArray2[6] = {24, 36, 43, 55, 60, 94}; + int testArray3[5] = {87, 91, 53, 37, 42}; + int answerArray3[5] = {37, 42, 53, 87, 91}; + int testArray4[1] = {5}; + int answerArray4[1] = {5}; + + if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) + || !testStarterForPartitionFunction(testArray2, answerArray2, 10) + || !testStarterForPartitionFunction(testArray3, answerArray3, 10) + || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) + { + return false; + } + + return true; +} + +bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size) +{ + if (binarySearch(testArray, elementWeAreLookingFor, 0, size -1) != answer) + { + return false; + } + + return true; +} + +bool testBinarySearch(void) +{ + int testArray1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int testElementWeAreLookingFor1 = 3; + int answer1 = 4; + + int testArray2[6] = {5, 4, 7, 2, 0, 10}; + int testElementWeAreLookingFor2 = 0; + int answer2 = 5; + + int testArray3[4] = {6, 42, 5, 8}; + int testElementWeAreLookingFor3 = 10; + int answer3 = -1; + + if (!testStarterForBinarySearch(testArray1, testElementWeAreLookingFor1, answer1, 10) + || !testStarterForBinarySearch(testArray2, testElementWeAreLookingFor2, answer2, 6) + || !testStarterForBinarySearch(testArray3, testElementWeAreLookingFor2, answer3, 4)) + { + return false; + } + + return true; +} + +bool testSmartQuickSort(void) +{ + +} \ No newline at end of file From bef8a09a78063b8bd2cdf8f1798817a4153e7804 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Tue, 3 Oct 2023 20:38:42 +0300 Subject: [PATCH 15/23] - --- semester_1/homework3/search.c | 135 ++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 22 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index da7cf38..cb44ccc 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -7,9 +7,9 @@ int partition(int array[], int leftElement, int rightElement); void swap(int *firstValue, int *secondValue); void printArray(int array[], int size); void generateRandomArray(int array[], int size); -void insertSort(int array[], int leftElement, int rightElement); +int insertSort(int array[], int leftElement, int rightElement); int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge); -void smartQuickSort(int array[], int leftElement, int rightElement); +int smartQuickSort(int array[], int leftElement, int rightElement); bool test(void); bool testSmartQuickSort(void); @@ -18,9 +18,8 @@ bool testSwapFunction(void); bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size); bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray); bool testBinarySearch(void); -bool testInsertSort(void); - - +bool testSortings(void); +bool checkIfArrayIsSorted(int array[], int size); int main() { @@ -72,7 +71,12 @@ int main() printArray(k_array, k); printf("\n"); - smartQuickSort(array, 0, size - 1); + int errorCode = smartQuickSort(array, 0, size - 1); + if (errorCode == 1) + { + printf("\nSorry but something went wrong\n"); + return 1; + } for (int i = 0; i < k; ++i) { @@ -127,12 +131,17 @@ int partition(int array[], int leftElement, int rightElement) return i; } -void smartQuickSort(int array[], int leftElement, int rightElement) +int smartQuickSort(int array[], int leftElement, int rightElement) { + if (rightElement < leftElement) + { + return 1; + } + if (rightElement - leftElement + 1 < 10) { insertSort(array, leftElement, rightElement + 1); - return; + return 0; } if (leftElement < rightElement) { @@ -140,6 +149,7 @@ void smartQuickSort(int array[], int leftElement, int rightElement) smartQuickSort(array, leftElement, pivotIndex - 1); smartQuickSort(array, pivotIndex + 1, rightElement); } + return 0; } void generateRandomArray(int array[], int size) @@ -150,8 +160,13 @@ void generateRandomArray(int array[], int size) } } -void insertSort(int array[], int leftElement, int rightElement) +int insertSort(int array[], int leftElement, int rightElement) { + if (rightElement < leftElement) + { + return 1; + } + for (int i = leftElement; i < rightElement; i++) { int currentElement = array[i]; @@ -163,6 +178,7 @@ void insertSort(int array[], int leftElement, int rightElement) } array[j + 1] = currentElement; } + return 0; } int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge) @@ -192,9 +208,7 @@ int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int righ bool test(void) { - if (!testSwapFunction() - || !testBinarySearch() - || !testPartitionFunction()) + if (!testSwapFunction() || !testBinarySearch() || !testPartitionFunction() || !testSortings()) { return false; } @@ -241,10 +255,7 @@ bool testsForPartitionFunction(void) int testArray4[1] = {5}; int answerArray4[1] = {5}; - if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) - || !testStarterForPartitionFunction(testArray2, answerArray2, 10) - || !testStarterForPartitionFunction(testArray3, answerArray3, 10) - || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) + if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) || !testStarterForPartitionFunction(testArray2, answerArray2, 10) || !testStarterForPartitionFunction(testArray3, answerArray3, 10) || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) { return false; } @@ -254,7 +265,7 @@ bool testsForPartitionFunction(void) bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size) { - if (binarySearch(testArray, elementWeAreLookingFor, 0, size -1) != answer) + if (binarySearch(testArray, elementWeAreLookingFor, 0, size - 1) != answer) { return false; } @@ -276,9 +287,7 @@ bool testBinarySearch(void) int testElementWeAreLookingFor3 = 10; int answer3 = -1; - if (!testStarterForBinarySearch(testArray1, testElementWeAreLookingFor1, answer1, 10) - || !testStarterForBinarySearch(testArray2, testElementWeAreLookingFor2, answer2, 6) - || !testStarterForBinarySearch(testArray3, testElementWeAreLookingFor2, answer3, 4)) + if (!testStarterForBinarySearch(testArray1, testElementWeAreLookingFor1, answer1, 10) || !testStarterForBinarySearch(testArray2, testElementWeAreLookingFor2, answer2, 6) || !testStarterForBinarySearch(testArray3, testElementWeAreLookingFor2, answer3, 4)) { return false; } @@ -286,7 +295,89 @@ bool testBinarySearch(void) return true; } -bool testSmartQuickSort(void) +bool testStarterForInsertSort(int testArray[], int size, int ec) { + int errorCode = insertSort(testArray, 0, size - 1); + if (ec == 1) + { + if (errorCode != 1) + { + return false; + } + return true; + } + if (errorCode != 0 || !checkIfArrayIsSorted(testArray, 10)) + { + return false; + } + return true; +} + +bool testStarterForSmartQuickSort(int testArray[], int size, int ec) +{ + int errorCode = smartQuickSort(testArray, 0, size - 1); + if (ec == 1) + { + if (errorCode != 1) + { + return false; + } + return true; + } + if (errorCode != 0 || !checkIfArrayIsSorted(testArray, 10)) + { + return false; + } + return true; +} + +bool testSortings(void) +{ + int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int errorCode1 = 0; + + int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + int errorCode2 = 0; + + int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; + int errorCode3 = 0; + + int testArray4[10] = {0}; + int errorCode4 = 0; + + int testArray5[1] = {1}; + int errorCode5 = 1; + + if (!testStarterForInsertSort(testArray1, 10, 0) + || !testStarterForInsertSort(testArray1, 10, 0) + || !testStarterForInsertSort(testArray2, 10, 0) + || !testStarterForInsertSort(testArray3, 10, 0) + || !testStarterForInsertSort(testArray4, 1, 1)) + { + return false; + } + + if (!testStarterForSmartQuickSort(testArray1, 10, 0) + || !testStarterForSmartQuickSort(testArray1, 10, 0) + || !testStarterForSmartQuickSort(testArray2, 10, 0) + || !testStarterForSmartQuickSort(testArray3, 10, 0) + || !testStarterForSmartQuickSort(testArray4, 1, 1)) + { + return false; + } -} \ No newline at end of file + return true; +} + +bool checkIfArrayIsSorted(int array[], int size) +{ + int previousElement = array[0]; + for (int i = 1; i < size; ++i) + { + if (previousElement > array[i]) + { + return false; + } + } + return true; +} From 8e890267beedf3cb7933f60f9d50153dd95c5cbd Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 4 Oct 2023 08:15:14 +0300 Subject: [PATCH 16/23] - --- semester_1/homework3/search.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index cb44ccc..ad3d34b 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -78,6 +78,12 @@ int main() return 1; } + if (errorCode == 2) + { + printf("\nsmartQuickSort: Incorrect input\n"); + return 1; + } + for (int i = 0; i < k; ++i) { int number = k_array[i]; @@ -135,12 +141,16 @@ int smartQuickSort(int array[], int leftElement, int rightElement) { if (rightElement < leftElement) { - return 1; + return 2; } if (rightElement - leftElement + 1 < 10) { - insertSort(array, leftElement, rightElement + 1); + int errorCode = insertSort(array, leftElement, rightElement + 1); + if (errorCode != 0) + { + return 1; + } return 0; } if (leftElement < rightElement) @@ -255,7 +265,10 @@ bool testsForPartitionFunction(void) int testArray4[1] = {5}; int answerArray4[1] = {5}; - if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) || !testStarterForPartitionFunction(testArray2, answerArray2, 10) || !testStarterForPartitionFunction(testArray3, answerArray3, 10) || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) + if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) + || !testStarterForPartitionFunction(testArray2, answerArray2, 10) + || !testStarterForPartitionFunction(testArray3, answerArray3, 10) + || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) { return false; } @@ -365,7 +378,7 @@ bool testSortings(void) { return false; } - + return true; } From 84899fa17e78a2673f2b5a82e00e92f8e976c90b Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 4 Oct 2023 13:51:28 +0300 Subject: [PATCH 17/23] - --- semester_1/homework3/search.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index ad3d34b..789a2db 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -12,14 +12,14 @@ int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int righ int smartQuickSort(int array[], int leftElement, int rightElement); bool test(void); -bool testSmartQuickSort(void); -bool testPartitionFunction(void); bool testSwapFunction(void); bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size); bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray); bool testBinarySearch(void); bool testSortings(void); bool checkIfArrayIsSorted(int array[], int size); +bool testPartitionFunction(void); +bool testStarterForSmartQuickSort(int testArray[], int size, int ec); int main() { @@ -251,7 +251,7 @@ bool testStarterForPartitionFunction(int testArray[], int answerArray[], int siz return true; } -bool testsForPartitionFunction(void) +bool testPartitionFunction(void) { int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; From 9beb0fd8c96e245c1986d0377001d9cc49b03924 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 4 Oct 2023 20:30:06 +0300 Subject: [PATCH 18/23] search --- semester_1/homework3/search.c | 520 ++++++++++++++-------------------- 1 file changed, 220 insertions(+), 300 deletions(-) diff --git a/semester_1/homework3/search.c b/semester_1/homework3/search.c index 789a2db..54a7cac 100644 --- a/semester_1/homework3/search.c +++ b/semester_1/homework3/search.c @@ -12,278 +12,285 @@ int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int righ int smartQuickSort(int array[], int leftElement, int rightElement); bool test(void); +bool testSmartQuickSort(void); +bool testPartitionFunction(void); bool testSwapFunction(void); bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size); -bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray); +bool testStarterForPartitionFunction(int testArray[], int answerArray[], int size); bool testBinarySearch(void); bool testSortings(void); bool checkIfArrayIsSorted(int array[], int size); -bool testPartitionFunction(void); -bool testStarterForSmartQuickSort(int testArray[], int size, int ec); +bool testStarterForArrays(int testArray[], int answerArray[], int size); int main() { - if (!test()) - { - printf("\nSorry but the program does not work correctly\n"); - return 1; - } - - srand(time(0)); - - printf("\nEnter the size of your array: "); - int size = 0; - scanf("%d", &size); - - while (size <= 0) - { - printf("\nSize of your array should be at least 1\n"); - printf("Enter the size of your array: "); - scanf("%d", &size); - } - - int *array = malloc(size * sizeof(int)); - if (array == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - generateRandomArray(array, size); - - printf("\nEnter the quantity of numbers for tests: "); - int k = 0; - scanf("%d", &k); - printf("\n"); - - int *k_array = malloc(k * sizeof(int)); - if (k_array == NULL) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - generateRandomArray(k_array, k); - - printf("Your array: "); - printArray(array, size); - printf("Your numbers to search in array: "); - printArray(k_array, k); - printf("\n"); - - int errorCode = smartQuickSort(array, 0, size - 1); - if (errorCode == 1) - { - printf("\nSorry but something went wrong\n"); - return 1; - } - - if (errorCode == 2) - { - printf("\nsmartQuickSort: Incorrect input\n"); - return 1; - } - - for (int i = 0; i < k; ++i) - { - int number = k_array[i]; - if (binarySearch(array, number, 0, size - 1) != -1) - { - printf("%d is in array\n", k_array[i]); - } - else - { - printf("%d is not in array\n", k_array[i]); - } - } - printf("\n"); - - return 0; + if (!test()) + { + return 1; + } + + printf("\nEnter the size of your array: "); + int size = 0; + scanf("%d", &size); + + while (size <= 0) + { + printf("\nSize of your array should be at least 1\n"); + printf("Enter the size of your array: "); + scanf("%d", &size); + } + + int *array = malloc(size * sizeof(int)); + if (array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + printf("Enter numbers of your array one by one each other on the next line: \n"); + + for (int i = 0; i < size; ++i) + { + printf("Enter %d number of your array: ", i + 1); + int currentNumber = 0; + scanf("%d", ¤tNumber); + array[i] = currentNumber; + } + + printf("\nYour array before getting sorted: \n"); + printArray(array, size); + + int errorCode = smartQuickSort(array, 0, size - 1); + if (errorCode == 1) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + if (errorCode == 2) + { + printf("\nsmartQuickSort: Incorrect input\n"); + return 1; + } + + printf("\nYour array after getting sorted: \n"); + printArray(array, size); + + return 0; } void printArray(int array[], int size) { - for (int i = 0; i < size; ++i) - { - printf("%d ", array[i]); - } - printf("\n"); + for (int i = 0; i < size; ++i) + { + printf("%d ", array[i]); + } + printf("\n"); } void swap(int *firstValue, int *secondValue) { - int buffer = *firstValue; - *firstValue = *secondValue; - *secondValue = buffer; + int buffer = *firstValue; + *firstValue = *secondValue; + *secondValue = buffer; } int partition(int array[], int leftElement, int rightElement) { - rightElement -= 1; - int currentSeparator = array[leftElement]; - int i = rightElement; - - for (int j = rightElement; j > leftElement; --j) - { - if (array[j] >= currentSeparator) - { - swap(&array[i], &array[j]); - --i; - } - } - - swap(&array[i], &array[leftElement]); - - return i; + rightElement -= 1; + int currentSeparator = array[leftElement]; + int i = rightElement; + + for (int j = rightElement; j > leftElement; --j) + { + if (array[j] >= currentSeparator) + { + swap(&array[i], &array[j]); + --i; + } + } + + swap(&array[i], &array[leftElement]); + + return i; } int smartQuickSort(int array[], int leftElement, int rightElement) { - if (rightElement < leftElement) - { - return 2; - } - - if (rightElement - leftElement + 1 < 10) - { - int errorCode = insertSort(array, leftElement, rightElement + 1); - if (errorCode != 0) - { - return 1; - } - return 0; - } - if (leftElement < rightElement) - { - int pivotIndex = partition(array, 0, rightElement); - smartQuickSort(array, leftElement, pivotIndex - 1); - smartQuickSort(array, pivotIndex + 1, rightElement); - } - return 0; + if (rightElement < leftElement) + { + return 2; + } + + if (rightElement - leftElement + 1 < 10) + { + int errorCode = insertSort(array, leftElement, rightElement + 1); + if (errorCode != 0) + { + return 1; + } + return 0; + } + if (leftElement < rightElement) + { + int currentSeparator = partition(array, 0, rightElement); + smartQuickSort(array, leftElement, currentSeparator - 1); + smartQuickSort(array, currentSeparator + 1, rightElement); + } + return 0; } void generateRandomArray(int array[], int size) { - for (int i = 0; i < size; ++i) - { - array[i] = rand() % 100; - } + for (int i = 0; i < size; ++i) + { + array[i] = rand() % 100; + } } int insertSort(int array[], int leftElement, int rightElement) { - if (rightElement < leftElement) - { - return 1; - } - - for (int i = leftElement; i < rightElement; i++) - { - int currentElement = array[i]; - int j = i - 1; - while (j >= leftElement && array[j] >= currentElement) - { - array[j + 1] = array[j]; - j--; - } - array[j + 1] = currentElement; - } - return 0; + if (rightElement < leftElement) + { + return 1; + } + + for (int i = leftElement; i < rightElement; ++i) + { + int currentElement = array[i]; + int j = i - 1; + while (j >= leftElement && array[j] >= currentElement) + { + array[j + 1] = array[j]; + --j; + } + array[j + 1] = currentElement; + } + return 0; } int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge) { - while (leftEdge <= rightEdge) - { - int middleElement = leftEdge + (rightEdge - leftEdge) / 2; - - if (array[middleElement] == elementWeAreLookingFor) - { - return middleElement; - } - - if (array[middleElement] < elementWeAreLookingFor) - { - leftEdge = middleElement + 1; - } - - else - { - rightEdge = middleElement - 1; - } - } - - return -1; + while (leftEdge <= rightEdge) + { + int middleElement = leftEdge + (rightEdge - leftEdge) / 2; + + if (array[middleElement] == elementWeAreLookingFor) + { + return middleElement; + } + + if (array[middleElement] < elementWeAreLookingFor) + { + leftEdge = middleElement + 1; + } + + else + { + rightEdge = middleElement - 1; + } + } + + return -1; } bool test(void) { - if (!testSwapFunction() || !testBinarySearch() || !testPartitionFunction() || !testSortings()) - { - return false; - } + if (!(testSwapFunction() * testPartitionFunction() * testSmartQuickSort() * testBinarySearch())) + { + return false; + } - return true; + return true; } bool testSwapFunction(void) { - int firstValue = 0; - int secondValue = 1; - swap(&firstValue, &secondValue); - if (firstValue != 1 || secondValue != 0) - { - return false; - } - return true; + int firstValue = 0; + int secondValue = 1; + swap(&firstValue, &secondValue); + if (firstValue != 1 || secondValue != 0) + { + return false; + } + return true; } -bool testStarterForPartitionFunction(int testArray[], int answerArray[], int sizeOfTestArray) +bool testStarterForArrays(int testArray[], int answerArray[], int size) { - partition(testArray, 0, sizeOfTestArray - 1); - for (int i = 0; i < 9; ++i) - { - if (testArray[i] != answerArray[i]) - { - return false; - } - } - return true; + for (int i = 0; i < size; ++i) + { + if (testArray[i] != answerArray[i]) + { + return false; + } + } + return true; } bool testPartitionFunction(void) { - int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; - int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; + int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; + int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; - int testArray2[6] = {43, 36, 55, 60, 94, 24}; - int answerArray2[6] = {24, 36, 43, 55, 60, 94}; + int testArray2[6] = {43, 36, 55, 60, 94, 24}; + int answerArray2[6] = {24, 36, 43, 55, 60, 94}; - int testArray3[5] = {87, 91, 53, 37, 42}; - int answerArray3[5] = {37, 42, 53, 87, 91}; + int testArray3[5] = {87, 91, 53, 37, 42}; + int answerArray3[5] = {37, 42, 53, 87, 91}; - int testArray4[1] = {5}; - int answerArray4[1] = {5}; + int testArray4[1] = {5}; + int answerArray4[1] = {5}; - if (!testStarterForPartitionFunction(testArray1, answerArray1, 10) - || !testStarterForPartitionFunction(testArray2, answerArray2, 10) - || !testStarterForPartitionFunction(testArray3, answerArray3, 10) - || !testStarterForPartitionFunction(testArray4, answerArray4, 10)) - { - return false; - } + partition(testArray1, 0, 9); + partition(testArray2, 0, 6); + partition(testArray3, 0, 5); + partition(testArray4, 0, 1); - return true; + if (!(testStarterForArrays(testArray1, answerArray1, 9) * testStarterForArrays(testArray2, answerArray2, 6) * testStarterForArrays(testArray3, answerArray3, 5) * testStarterForArrays(testArray4, answerArray4, 1))) + { + return false; + } + + return true; } -bool testStarterForBinarySearch(int testArray[], int elementWeAreLookingFor, int answer, int size) +bool testSmartQuickSort(void) { - if (binarySearch(testArray, elementWeAreLookingFor, 0, size - 1) != answer) - { - return false; - } - return true; + int testArray1[5] = {5, 4, 3, 2, 1}; + int answerArray1[5] = {1, 2, 3, 4, 5}; + + int testArray2[5] = {1, 2, 3, 4, 5}; + int answerArray2[5] = {1, 2, 3, 4, 5}; + + int testArray3[1] = {1}; + int answerArray3[1] = {1}; + + int errorCode = smartQuickSort(testArray1, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray2, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray3, 0, 1 - 1); + if (errorCode != 0) + { + return false; + } + + if (!(testStarterForArrays(testArray1, answerArray1, 5) * testStarterForArrays(testArray2, answerArray2, 5) * testStarterForArrays(testArray3, answerArray3, 1))) + { + return false; + } + + return true; } bool testBinarySearch(void) @@ -300,97 +307,10 @@ bool testBinarySearch(void) int testElementWeAreLookingFor3 = 10; int answer3 = -1; - if (!testStarterForBinarySearch(testArray1, testElementWeAreLookingFor1, answer1, 10) || !testStarterForBinarySearch(testArray2, testElementWeAreLookingFor2, answer2, 6) || !testStarterForBinarySearch(testArray3, testElementWeAreLookingFor2, answer3, 4)) + if (!(testStarterForBinarySearch(testArray1, testElementWeAreLookingFor1, answer1, 10) || !testStarterForBinarySearch(testArray2, testElementWeAreLookingFor2, answer2, 6) || !testStarterForBinarySearch(testArray3, testElementWeAreLookingFor2, answer3, 4))) { return false; } return true; -} - -bool testStarterForInsertSort(int testArray[], int size, int ec) -{ - int errorCode = insertSort(testArray, 0, size - 1); - if (ec == 1) - { - if (errorCode != 1) - { - return false; - } - return true; - } - if (errorCode != 0 || !checkIfArrayIsSorted(testArray, 10)) - { - return false; - } - return true; -} - -bool testStarterForSmartQuickSort(int testArray[], int size, int ec) -{ - int errorCode = smartQuickSort(testArray, 0, size - 1); - if (ec == 1) - { - if (errorCode != 1) - { - return false; - } - return true; - } - if (errorCode != 0 || !checkIfArrayIsSorted(testArray, 10)) - { - return false; - } - return true; -} - -bool testSortings(void) -{ - int testArray1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int errorCode1 = 0; - - int testArray2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; - int errorCode2 = 0; - - int testArray3[10] = {4, 8, 2, 3, 1, 9, 7, 5, 10, 6}; - int errorCode3 = 0; - - int testArray4[10] = {0}; - int errorCode4 = 0; - - int testArray5[1] = {1}; - int errorCode5 = 1; - - if (!testStarterForInsertSort(testArray1, 10, 0) - || !testStarterForInsertSort(testArray1, 10, 0) - || !testStarterForInsertSort(testArray2, 10, 0) - || !testStarterForInsertSort(testArray3, 10, 0) - || !testStarterForInsertSort(testArray4, 1, 1)) - { - return false; - } - - if (!testStarterForSmartQuickSort(testArray1, 10, 0) - || !testStarterForSmartQuickSort(testArray1, 10, 0) - || !testStarterForSmartQuickSort(testArray2, 10, 0) - || !testStarterForSmartQuickSort(testArray3, 10, 0) - || !testStarterForSmartQuickSort(testArray4, 1, 1)) - { - return false; - } - - return true; -} - -bool checkIfArrayIsSorted(int array[], int size) -{ - int previousElement = array[0]; - for (int i = 1; i < size; ++i) - { - if (previousElement > array[i]) - { - return false; - } - } - return true; -} +} \ No newline at end of file From 956313dcd5e9fa72caa2e85b1c1340945f7d21f3 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 4 Oct 2023 21:11:47 +0300 Subject: [PATCH 19/23] mostFrequentElement task --- semester_1/homework3/mostFrequentElement.c | 282 +++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 semester_1/homework3/mostFrequentElement.c diff --git a/semester_1/homework3/mostFrequentElement.c b/semester_1/homework3/mostFrequentElement.c new file mode 100644 index 0000000..e89425f --- /dev/null +++ b/semester_1/homework3/mostFrequentElement.c @@ -0,0 +1,282 @@ +#include +#include +#include + +int partition(int array[], int leftElement, int rightElement); +void swap(int *firstValue, int *secondValue); +int insertSort(int array[], int leftElement, int rightElement); +int smartQuickSort(int array[], int leftElement, int rightElement); +int mostFrequentElement(int array[], int size); + +bool test(void); +bool testSmartQuickSort(void); +bool testPartitionFunction(void); +bool testSwapFunction(void); +bool testStarterForPartitionFunction(int testArray[], int answerArray[], int size); +bool testStarterForMostFrequentELement(int testArray[], int size, int answer); +bool testMostFrequentElementFunction(void); + +int main() +{ + if (!test()) + { + printf("\nSorry but the program does not work correctly\n"); + return 1; + } + + printf("\nEnter the size of your array: "); + int size = 0; + scanf("%d", &size); + + while (size <= 0) + { + printf("\nSize of your array should be at least 1\n"); + printf("Enter the size of your array: "); + scanf("%d", &size); + } + + int *array = malloc(size * sizeof(int)); + if (array == NULL) + { + printf("\nSorry but something went wrong\n"); + return 1; + } + + printf("Enter numbers of your array one by one each other on the next line: \n"); + + for (int i = 0; i < size; ++i) + { + printf("Enter %d number of your array: ", i + 1); + int currentNumber = 0; + scanf("%d", ¤tNumber); + array[i] = currentNumber; + } + + printf("\nThe most frequent element in the array is %d\n", mostFrequentElement(array, size)); + + return 0; +} + +int mostFrequentElement(int array[], int size) +{ + int mostFrequentElement = array[0]; + int counter = 1; + int maxCount = 1; + for (int i = 1; i < size; ++i) + { + if (array[i] == array[i - 1]) + { + ++counter; + if (counter > maxCount) + { + maxCount = counter; + mostFrequentElement = array[i]; + } + } + else + { + counter = 1; + } + } + return mostFrequentElement; +} + +int smartQuickSort(int array[], int leftElement, int rightElement) +{ + if (rightElement < leftElement) + { + return 2; + } + + if (rightElement - leftElement + 1 < 10) + { + int errorCode = insertSort(array, leftElement, rightElement + 1); + if (errorCode != 0) + { + return 1; + } + return 0; + } + if (leftElement < rightElement) + { + int currentSeparator = partition(array, 0, rightElement); + smartQuickSort(array, leftElement, currentSeparator - 1); + smartQuickSort(array, currentSeparator + 1, rightElement); + } + return 0; +} + +int insertSort(int array[], int leftElement, int rightElement) +{ + if (rightElement < leftElement) + { + return 1; + } + + for (int i = leftElement; i < rightElement; ++i) + { + int currentElement = array[i]; + int j = i - 1; + while (j >= leftElement && array[j] >= currentElement) + { + array[j + 1] = array[j]; + --j; + } + array[j + 1] = currentElement; + } + return 0; +} + +int partition(int array[], int leftElement, int rightElement) +{ + rightElement -= 1; + int currentSeparator = array[leftElement]; + int i = rightElement; + + for (int j = rightElement; j > leftElement; --j) + { + if (array[j] >= currentSeparator) + { + swap(&array[i], &array[j]); + --i; + } + } + + swap(&array[i], &array[leftElement]); + + return i; +} + +void swap(int *firstValue, int *secondValue) +{ + int buffer = *firstValue; + *firstValue = *secondValue; + *secondValue = buffer; +} + +bool test(void) +{ + if (!(testSwapFunction() * testPartitionFunction() * testSmartQuickSort() * testMostFrequentElementFunction())) + { + return false; + } + + return true; +} + +bool testSwapFunction(void) +{ + int firstValue = 0; + int secondValue = 1; + swap(&firstValue, &secondValue); + if (firstValue != 1 || secondValue != 0) + { + return false; + } + return true; +} + +bool testStarterForArrays(int testArray[], int answerArray[], int size) +{ + for (int i = 0; i < size; ++i) + { + if (testArray[i] != answerArray[i]) + { + return false; + } + } + return true; +} + +bool testPartitionFunction(void) +{ + int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; + int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; + + int testArray2[6] = {43, 36, 55, 60, 94, 24}; + int answerArray2[6] = {24, 36, 43, 55, 60, 94}; + + int testArray3[5] = {87, 91, 53, 37, 42}; + int answerArray3[5] = {37, 42, 53, 87, 91}; + + int testArray4[1] = {5}; + int answerArray4[1] = {5}; + + partition(testArray1, 0, 9); + partition(testArray2, 0, 6); + partition(testArray3, 0, 5); + partition(testArray4, 0, 1); + + if (!(testStarterForArrays(testArray1, answerArray1, 9) * testStarterForArrays(testArray2, answerArray2, 6) * testStarterForArrays(testArray3, answerArray3, 5) * testStarterForArrays(testArray4, answerArray4, 1))) + { + return false; + } + + return true; +} + +bool testSmartQuickSort(void) +{ + + int testArray1[5] = {5, 4, 3, 2, 1}; + int answerArray1[5] = {1, 2, 3, 4, 5}; + + int testArray2[5] = {1, 2, 3, 4, 5}; + int answerArray2[5] = {1, 2, 3, 4, 5}; + + int testArray3[1] = {1}; + int answerArray3[1] = {1}; + + int errorCode = smartQuickSort(testArray1, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray2, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray3, 0, 1 - 1); + if (errorCode != 0) + { + return false; + } + + if (!(testStarterForArrays(testArray1, answerArray1, 5) * testStarterForArrays(testArray2, answerArray2, 5) * testStarterForArrays(testArray3, answerArray3, 1))) + { + return false; + } + + return true; +} + +bool testMostFrequentElementFunction(void) +{ + int testArray1[10] = {1, 3, 4, 4, 5, 6, 7, 7, 7, 8}; + int answer1 = 7; + + int testArray2[4] = {3, 0, 0, 5}; + int answer2 = 0; + + int testArray3[1] = {0}; + int answer3 = 0; + + if (!(testStarterForMostFrequentELement(testArray1, 10, answer1) * testStarterForMostFrequentELement(testArray2, 4, answer2) * testStarterForMostFrequentELement(testArray3, 1, answer3))) + { + return false; + } + + return true; +} + +bool testStarterForMostFrequentELement(int testArray[], int size, int answer) +{ + if (mostFrequentElement(testArray, size) != answer) + { + return false; + } + return true; +} \ No newline at end of file From 1b6093d2eac010adeb329256a1ab183827f57b45 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Thu, 5 Oct 2023 16:26:15 +0300 Subject: [PATCH 20/23] mostFrequentElement2 --- .../homework4/mostFrequentElement2/file.txt | 10 + .../homework4/mostFrequentElement2/main.c | 120 ++++++++++++ .../mostFrequentElement2/smartQuickSort.c | 178 ++++++++++++++++++ .../mostFrequentElement2/smartQuickSort.h | 9 + 4 files changed, 317 insertions(+) create mode 100644 semester_1/homework4/mostFrequentElement2/file.txt create mode 100644 semester_1/homework4/mostFrequentElement2/main.c create mode 100644 semester_1/homework4/mostFrequentElement2/smartQuickSort.c create mode 100644 semester_1/homework4/mostFrequentElement2/smartQuickSort.h diff --git a/semester_1/homework4/mostFrequentElement2/file.txt b/semester_1/homework4/mostFrequentElement2/file.txt new file mode 100644 index 0000000..0a76b27 --- /dev/null +++ b/semester_1/homework4/mostFrequentElement2/file.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +2 +3 +1 +3 +2 \ No newline at end of file diff --git a/semester_1/homework4/mostFrequentElement2/main.c b/semester_1/homework4/mostFrequentElement2/main.c new file mode 100644 index 0000000..8ab4cba --- /dev/null +++ b/semester_1/homework4/mostFrequentElement2/main.c @@ -0,0 +1,120 @@ +#include "smartQuickSort.h" + +#include +#include +#include + +int mostFrequentElement(int array[], int size); +void printArray(int array[], int size); + +bool test(void); +bool testStarterForMostFrequentELement(int testArray[], int size, int answer); +bool testMostFrequentElementFunction(void); + +int main() +{ + if (!test()) + { + printf("\nSorry but the program does not work correctly\n"); + return 1; + } + + FILE *file = fopen("file.txt", "r"); + if (file == NULL) + { + return 1; + } + + int array[100] = {0}; + + int i = 0; + while (fscanf(file,"%d" , &(array[i])) != EOF) + { + ++i; + } + + fclose(file); + + + printf("Numbers from the file:\n"); + printArray(array, i); + + int errorCode = smartQuickSort(array, 0, i - 1); + + printf("\nThe most frequent element in the file is %d\n", mostFrequentElement(array, i + 1)); + + + + return 0; +} + +void printArray(int array[], int size) +{ + for (int i = 0; i < size; ++i) + { + printf("%d ", array[i]); + } + printf("\n"); +} + +int mostFrequentElement(int array[], int size) +{ + int mostFrequentElement = array[0]; + int counter = 1; + int maxCount = 1; + for (int i = 1; i < size; ++i) + { + if (array[i] == array[i - 1]) + { + ++counter; + if (counter > maxCount) + { + maxCount = counter; + mostFrequentElement = array[i]; + } + } + else + { + counter = 1; + } + } + return mostFrequentElement; +} + +bool test(void) +{ + if (!(testMostFrequentElementFunction() * testSmartQuickSort())) + { + return false; + } + + return true; +} + +bool testMostFrequentElementFunction(void) +{ + int testArray1[10] = {1, 3, 4, 4, 5, 6, 7, 7, 7, 8}; + int answer1 = 7; + + int testArray2[4] = {3, 0, 0, 5}; + int answer2 = 0; + + int testArray3[1] = {0}; + int answer3 = 0; + + if (!(testStarterForMostFrequentELement(testArray1, 10, answer1) * testStarterForMostFrequentELement(testArray2, 4, answer2) * testStarterForMostFrequentELement(testArray3, 1, answer3))) + { + return false; + } + + return true; +} + +bool testStarterForMostFrequentELement(int testArray[], int size, int answer) +{ + if (mostFrequentElement(testArray, size) != answer) + { + return false; + } + return true; +} \ No newline at end of file diff --git a/semester_1/homework4/mostFrequentElement2/smartQuickSort.c b/semester_1/homework4/mostFrequentElement2/smartQuickSort.c new file mode 100644 index 0000000..64b35e2 --- /dev/null +++ b/semester_1/homework4/mostFrequentElement2/smartQuickSort.c @@ -0,0 +1,178 @@ +#include "smartQuickSort.h" + +#include + + +int smartQuickSort(int array[], int leftElement, int rightElement) +{ + if (rightElement < leftElement) + { + return 2; + } + + if (rightElement - leftElement + 1 < 10) + { + int errorCode = insertSort(array, leftElement, rightElement + 1); + if (errorCode != 0) + { + return 1; + } + return 0; + } + if (leftElement < rightElement) + { + int currentSeparator = partition(array, 0, rightElement); + smartQuickSort(array, leftElement, currentSeparator - 1); + smartQuickSort(array, currentSeparator + 1, rightElement); + } + return 0; +} + +int insertSort(int array[], int leftElement, int rightElement) +{ + if (rightElement < leftElement) + { + return 1; + } + + for (int i = leftElement; i < rightElement; ++i) + { + int currentElement = array[i]; + int j = i - 1; + while (j >= leftElement && array[j] >= currentElement) + { + array[j + 1] = array[j]; + --j; + } + array[j + 1] = currentElement; + } + return 0; +} + +int partition(int array[], int leftElement, int rightElement) +{ + rightElement -= 1; + int currentSeparator = array[leftElement]; + int i = rightElement; + + for (int j = rightElement; j > leftElement; --j) + { + if (array[j] >= currentSeparator) + { + swap(&array[i], &array[j]); + --i; + } + } + + swap(&array[i], &array[leftElement]); + + return i; +} + +void swap(int *firstValue, int *secondValue) +{ + int buffer = *firstValue; + *firstValue = *secondValue; + *secondValue = buffer; +} + + + +bool testSmartQuickSort(void) +{ + if (!(testSwapFunction() * testPartitionFunction() * testSmartQuickSortFunction())) + { + return false; + } + + return true; +} + +bool testSwapFunction(void) +{ + int firstValue = 0; + int secondValue = 1; + swap(&firstValue, &secondValue); + if (firstValue != 1 || secondValue != 0) + { + return false; + } + return true; +} + +bool testStarterForArrays(int testArray[], int answerArray[], int size) +{ + for (int i = 0; i < size; ++i) + { + if (testArray[i] != answerArray[i]) + { + return false; + } + } + return true; +} + +bool testPartitionFunction(void) +{ + int testArray1[9] = {61, 99, 60, 80, 27, 57, 35, 81, 80}; + int answerArray1[9] = {27, 57, 60, 35, 61, 99, 80, 81, 80}; + + int testArray2[6] = {43, 36, 55, 60, 94, 24}; + int answerArray2[6] = {24, 36, 43, 55, 60, 94}; + + int testArray3[5] = {87, 91, 53, 37, 42}; + int answerArray3[5] = {37, 42, 53, 87, 91}; + + int testArray4[1] = {5}; + int answerArray4[1] = {5}; + + partition(testArray1, 0, 9); + partition(testArray2, 0, 6); + partition(testArray3, 0, 5); + partition(testArray4, 0, 1); + + if (!(testStarterForArrays(testArray1, answerArray1, 9) * testStarterForArrays(testArray2, answerArray2, 6) * testStarterForArrays(testArray3, answerArray3, 5) * testStarterForArrays(testArray4, answerArray4, 1))) + { + return false; + } + + return true; +} + +bool testSmartQuickSortFunction(void) +{ + + int testArray1[5] = {5, 4, 3, 2, 1}; + int answerArray1[5] = {1, 2, 3, 4, 5}; + + int testArray2[5] = {1, 2, 3, 4, 5}; + int answerArray2[5] = {1, 2, 3, 4, 5}; + + int testArray3[1] = {1}; + int answerArray3[1] = {1}; + + int errorCode = smartQuickSort(testArray1, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray2, 0, 5 - 1); + if (errorCode != 0) + { + return false; + } + + errorCode = smartQuickSort(testArray3, 0, 1 - 1); + if (errorCode != 0) + { + return false; + } + + if (!(testStarterForArrays(testArray1, answerArray1, 5) * testStarterForArrays(testArray2, answerArray2, 5) * testStarterForArrays(testArray3, answerArray3, 1))) + { + return false; + } + + return true; +} diff --git a/semester_1/homework4/mostFrequentElement2/smartQuickSort.h b/semester_1/homework4/mostFrequentElement2/smartQuickSort.h new file mode 100644 index 0000000..c443488 --- /dev/null +++ b/semester_1/homework4/mostFrequentElement2/smartQuickSort.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +// smart quick sort main function +int smartQuickSort(int array[], int leftElement, int rightElement); + +// a function for testing the work of quick sort +bool testSmartQuickSort(void); \ No newline at end of file From 3ebfb9135abef27198773292e6829cbb08061d01 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 9 Oct 2023 18:33:46 +0300 Subject: [PATCH 21/23] decimalTOBinary function --- .../binaryRepresentation.c | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 semester_1/homework4/binaryRepresentation/binaryRepresentation.c diff --git a/semester_1/homework4/binaryRepresentation/binaryRepresentation.c b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c new file mode 100644 index 0000000..4a3a0a8 --- /dev/null +++ b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c @@ -0,0 +1,40 @@ +#include +#include + +#define SIZE sizeof(int) * 8 + +int decimalToBinary(int number, int answer[]); +void printBinary(int array[]); + +int main(int argc, char const *argv[]) +{ + int number = 4; + int answer[SIZE] = {0}; + + int errorCode = decimalToBinary(number, answer); + + printBinary(answer); + + + return 0; +} + +int decimalToBinary(int number, int answer[]) +{ + int bit = 1; + for (int i = SIZE - 1; i < SIZE; --i) + { + answer[i] = ((number & bit) ? 1: 0); + number = number >> 1; + } + return 0; +} + +void printBinary(int array[]) +{ + for (int i = 0; i < SIZE; ++i) + { + printf("%d ", array[i]); + } + printf("\n"); +} \ No newline at end of file From d409fac40641de5a1f91f2a4bcaf8353fa6a0baf Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 11 Oct 2023 14:17:29 +0300 Subject: [PATCH 22/23] =?UTF-8?q?=D0=94=D0=B2=D0=BE=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D0=BE=D0=B5=20=D0=BF=D1=80=D0=B5=D0=B4=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binaryRepresentation.c | 301 +++++++++++++++++- 1 file changed, 293 insertions(+), 8 deletions(-) diff --git a/semester_1/homework4/binaryRepresentation/binaryRepresentation.c b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c index 4a3a0a8..2ed822c 100644 --- a/semester_1/homework4/binaryRepresentation/binaryRepresentation.c +++ b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c @@ -1,21 +1,91 @@ #include #include +#include #define SIZE sizeof(int) * 8 int decimalToBinary(int number, int answer[]); -void printBinary(int array[]); +void printBinary(const int array[]); +int binaryAddition(int binary1[], int binary2[], int answer[]); + +int binaryToDecimal(const int binNum[]); + +bool test(void); +bool testDecimalToBinary(void); +bool testBinaryToDecimal(void); +bool testBinaryAdditionFunction(void); +bool testStarterDecimalToBinary(int test, const int answer[], int size); +bool testStarterAddition(int a, int b, int answer); int main(int argc, char const *argv[]) { - int number = 4; - int answer[SIZE] = {0}; + if (!test()) + { + printf("Похоже, что сейчас программа не работает корректно"); + return 1; + } - int errorCode = decimalToBinary(number, answer); + setlocale(LC_ALL, "Ru.866"); - printBinary(answer); + printf("Сейчас вам нужно будет ввести два числа\n"); + + printf("Введите первое число: "); + int number1 = 0; + int elementsScanned1 = scanf("%d", &number1); + if (elementsScanned1 != 1) + { + printf("Похоже, что-то пошло не так\n"); + return 1; + } + + printf("Введите второе число: "); + int number2 = 0; + int elementsScanned2 = scanf("%d", &number2); + if (elementsScanned2 != 1) + { + printf("Похоже, что-то пошло не так\n"); + return 1; + } + int exp = 2; + for (int i = 0; i < SIZE; ++i) + { + exp = exp * 2; + } + + int binary1[SIZE] = {0}; + int binary2[SIZE] = {0}; + + int errorCode1 = decimalToBinary(number1, binary1); + if (errorCode1 != 0) + { + printf("Похоже, что-то пошло не так\n"); + return 1; + } + + int errorCode2 = decimalToBinary(number2, binary2); + if (errorCode2 != 0) + { + printf("Похоже, что-то пошло не так\n"); + } + + printBinary(binary1); + printBinary(binary2); + + int additionAnswer[SIZE] = {0}; + + int errorCode3 = binaryAddition(binary1, binary2, additionAnswer); + if (errorCode3 != 0) + { + printf("Похоже, что-то пошло не так\n"); + return 1; + } + + printf("Сумма в двоичном представлении:\n"); + printBinary(additionAnswer); + int ans = binaryToDecimal(additionAnswer); + + printf("%d", ans); - return 0; } @@ -24,17 +94,232 @@ int decimalToBinary(int number, int answer[]) int bit = 1; for (int i = SIZE - 1; i < SIZE; --i) { - answer[i] = ((number & bit) ? 1: 0); + answer[i] = ((number & bit) ? 1 : 0); number = number >> 1; } return 0; } -void printBinary(int array[]) +int binaryAddition(int binary1[], int binary2[], int answer[]) +{ + int buffer = 0; + for (int i = SIZE - 1; i < SIZE; --i) + { + int currentNumber = binary1[i] + binary2[i] + buffer; + buffer = 0; + if (currentNumber == 2) + { + buffer = 1; + currentNumber = 0; + } + if (currentNumber == 3) + { + buffer = 1; + currentNumber = 1; + } + answer[i] = currentNumber; + } + return 0; +} + +void printBinary(const int array[]) { for (int i = 0; i < SIZE; ++i) { printf("%d ", array[i]); } printf("\n"); +} + +int binaryToDecimal(const int binNum[]) +{ + int multiplier = 1; + int result = 0; + for (int i = SIZE - 1; i >= 0; --i) + { + result += binNum[i] * multiplier; + multiplier *= 2; + } + return result; +} + +bool test(void) +{ + if (!(testBinaryAdditionFunction() * testDecimalToBinary() * testBinaryToDecimal())) + { + return false; + } + return true; +} + +bool testDecimalToBinary(void) +{ + int test1 = 5; + int answer1[3] = {1, 0, 1}; + bool testAnswer1 = testStarterDecimalToBinary(test1, answer1, 3); + + int test2 = 10; + int answer2[4] = {1, 0, 1, 0}; + bool testAnswer2 = testStarterDecimalToBinary(test2, answer2, 4); + + int test3 = 0; + int answer3[1] = {0}; + bool testAnswer3 = testStarterDecimalToBinary(test3, answer3, 1); + + int test4 = -5; + int answer4[3] = {0, 1, 1}; + bool testAnswer4 = testStarterDecimalToBinary(test4, answer4, 3); + + if (!(testAnswer1 * testAnswer2 * testAnswer3 * testAnswer4)) + { + return false; + } + + return true; +} + +bool testStarterDecimalToBinary(int test, const int answer[], int size) +{ + int binary[SIZE] = {0}; + int errorCode = decimalToBinary(test, binary); + if (errorCode != 0) + { + return false; + } + + int j = size; + for (int i = SIZE - 1; i >= SIZE - size; --i) + { + --j; + if (answer[j] != binary[i]) + { + printf("\n%d\n", j); + return false; + } + } + + if (test >= 0) + { + for (int i = SIZE - 1 - size; i >= 0; --i) + { + if (binary[i] == 1) + { + return false; + } + } + } + + if (test < 0) + { + for (int i = SIZE - 1 - size; i >= 0; --i) + { + if (binary[i] == 0) + { + return false; + } + } + } + + return true; +} + +bool testBinaryToDecimal(void) +{ + int test1 = 5; + int binary1[SIZE] = {0}; + int errorCode1 = decimalToBinary(test1, binary1); + if (errorCode1 != 0) + { + return false; + } + + int test2 = 2; + int binary2[SIZE] = {0}; + int errorCode2 = decimalToBinary(test2, binary2); + if (errorCode2 != 0) + { + return false; + } + + int test3 = 0; + int binary3[SIZE] = {0}; + int errorCode3 = decimalToBinary(test3, binary3); + if (errorCode3 != 0) + { + return false; + } + + int test4 = -5; + int binary4[SIZE] = {0}; + int errorCode4 = decimalToBinary(test4, binary4); + if (errorCode4 != 0) + { + return false; + } + + if (!(test1 == binaryToDecimal(binary1)) * (test2 == binaryToDecimal(binary2)) * (test3 == binaryToDecimal(binary3)) * (test4 == binaryToDecimal(binary4))) + { + return false; + } + + return true; +} + +bool testBinaryAdditionFunction(void) +{ + int a1 = 5; + int b1 = 10; + int answer1 = 15; + bool test1 = testStarterAddition(a1, b1, answer1); + + int a2 = 4; + int b2 = -4; + int answer2 = 0; + bool test2 = testStarterAddition(a2, b2, answer2); + + int a3 = 6; + int b3 = -2; + int answer3 = 4; + bool test3 = testStarterAddition(a3, b3, answer3); + + if (!(test1 * test2 * test3)) + { + return false; + } + + return true; +} + +bool testStarterAddition(int a, int b, int answer) +{ + int binaryA[SIZE] = {0}; + int binaryB[SIZE] = {0}; + int errorCodeA = decimalToBinary(a, binaryA); + if (errorCodeA != 0) + { + return false; + } + + int errorCodeB = decimalToBinary(b, binaryB); + if (errorCodeB != 0) + { + return false; + } + + int binaryAnswer[SIZE] = {0}; + + int errorCodeAddition = binaryAddition(binaryA, binaryB, binaryAnswer); + if (errorCodeAddition != 0) + { + return false; + } + + int decimalAnswer = binaryToDecimal(binaryAnswer); + + if (decimalAnswer != answer) + { + return false; + } + + return true; } \ No newline at end of file From afb3eb5010e348718af3208f07901d5fc4325a91 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Wed, 11 Oct 2023 14:20:09 +0300 Subject: [PATCH 23/23] - --- .../homework4/binaryRepresentation/binaryRepresentation.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/semester_1/homework4/binaryRepresentation/binaryRepresentation.c b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c index 2ed822c..63fdb50 100644 --- a/semester_1/homework4/binaryRepresentation/binaryRepresentation.c +++ b/semester_1/homework4/binaryRepresentation/binaryRepresentation.c @@ -84,7 +84,8 @@ int main(int argc, char const *argv[]) printBinary(additionAnswer); int ans = binaryToDecimal(additionAnswer); - printf("%d", ans); + printf("Сумма в десятичном представлении:\n"); + printf("%d\n", ans); return 0; }