From 72ca55ca0f5c43162ff657b913747e0780e7d0ae Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 21 Dec 2021 12:35:30 +0300 Subject: [PATCH 1/3] Task1 --- Task1Offset/Task1Offset.sln | 31 ++++ Task1Offset/Task1Offset/ASD.C | 45 ++++++ Task1Offset/Task1Offset/CountSort.c | 50 ++++++ Task1Offset/Task1Offset/CountSort.h | 4 + Task1Offset/Task1Offset/Task1Offset.vcxproj | 153 ++++++++++++++++++ .../Task1Offset/Task1Offset.vcxproj.filters | 36 +++++ Task1Offset/Task1Offset/TestCountSort.c | 74 +++++++++ Task1Offset/Task1Offset/TestCountSort.h | 7 + 8 files changed, 400 insertions(+) create mode 100644 Task1Offset/Task1Offset.sln create mode 100644 Task1Offset/Task1Offset/ASD.C create mode 100644 Task1Offset/Task1Offset/CountSort.c create mode 100644 Task1Offset/Task1Offset/CountSort.h create mode 100644 Task1Offset/Task1Offset/Task1Offset.vcxproj create mode 100644 Task1Offset/Task1Offset/Task1Offset.vcxproj.filters create mode 100644 Task1Offset/Task1Offset/TestCountSort.c create mode 100644 Task1Offset/Task1Offset/TestCountSort.h diff --git a/Task1Offset/Task1Offset.sln b/Task1Offset/Task1Offset.sln new file mode 100644 index 0000000..620ecb6 --- /dev/null +++ b/Task1Offset/Task1Offset.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Task1Offset", "Task1Offset\Task1Offset.vcxproj", "{40F6E0D7-3ED6-4169-86F0-26255F01154F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Debug|x64.ActiveCfg = Debug|x64 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Debug|x64.Build.0 = Debug|x64 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Debug|x86.ActiveCfg = Debug|Win32 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Debug|x86.Build.0 = Debug|Win32 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Release|x64.ActiveCfg = Release|x64 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Release|x64.Build.0 = Release|x64 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Release|x86.ActiveCfg = Release|Win32 + {40F6E0D7-3ED6-4169-86F0-26255F01154F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {14B11405-06B8-4D17-A9F1-63308F608222} + EndGlobalSection +EndGlobal diff --git a/Task1Offset/Task1Offset/ASD.C b/Task1Offset/Task1Offset/ASD.C new file mode 100644 index 0000000..1a80671 --- /dev/null +++ b/Task1Offset/Task1Offset/ASD.C @@ -0,0 +1,45 @@ +#include +#include +#include +#include "CountSort.h" +#include "TestCountSort.h" + +int main() +{ + if (!checkTheArrayCounter() || !correctCountSort()) + { + printf("Test failed"); + return -1; + } + int temporary = 1; + int counter = 0; + int minimum = INT_MAX; + int maximum = INT_MIN; + int array[1000] = { 0 }; + while (temporary != 0) + { + printf("enter the %d number\n", counter); + scanf_s("%d\n", &temporary); + array[counter] = temporary; + if (array[counter] < minimum) + { + minimum = array[counter]; + } + if (array[counter] > maximum) + { + maximum = array[counter]; + } + counter++; + } + int* count = countSortArray(array, 10); + printf("\n"); + for (int i = 0; i < 10; i++) + { + if (i >= 1 && array[i] == array[i - 1]) + { + continue; + } + printf("%d - %d\n", array[i], count[array[i] - minimum]); + } + +} \ No newline at end of file diff --git a/Task1Offset/Task1Offset/CountSort.c b/Task1Offset/Task1Offset/CountSort.c new file mode 100644 index 0000000..e2ab41c --- /dev/null +++ b/Task1Offset/Task1Offset/CountSort.c @@ -0,0 +1,50 @@ +#include "CountSort.h" +#include +#include + +// Function for sorting by counting +int* countSortArray(int* arrayOfNumbers, int numberOfElements) +{ + // Finding the minimum element in the array + int minimumElementInArray = arrayOfNumbers[0]; + for (int i = 1; i <= numberOfElements - 1; i++) + { + if (arrayOfNumbers[i] < minimumElementInArray) + { + minimumElementInArray = arrayOfNumbers[i]; + } + } + + // Finding the maximum element in the array + int maximumElementInArray = arrayOfNumbers[0]; + for (int i = 1; i <= numberOfElements - 1; i++) + { + if (arrayOfNumbers[i] > maximumElementInArray) + { + maximumElementInArray = arrayOfNumbers[i]; + } + } + + // Allocation of dynamic memory for a counter array + int* arrayOfCount = (int*)calloc((maximumElementInArray - minimumElementInArray + 1), sizeof(int)); + if (arrayOfCount == NULL) + { + return -1; + } + // For each element, we increase the counter for its value + for (int i = 0; i <= numberOfElements - 1; i++) + { + arrayOfCount[arrayOfNumbers[i] - minimumElementInArray]++; + } + int distributionounter = 0; + // For each i from the minimum value to the maximum, we write its value to the array sequentially arrayOfCount times + for (int i = minimumElementInArray; i <= maximumElementInArray; i++) + { + for (int j = 0; j < arrayOfCount[i - minimumElementInArray]; j++) + { + arrayOfNumbers[distributionounter] = i; + distributionounter++; + } + } + return arrayOfCount; +} \ No newline at end of file diff --git a/Task1Offset/Task1Offset/CountSort.h b/Task1Offset/Task1Offset/CountSort.h new file mode 100644 index 0000000..65c817e --- /dev/null +++ b/Task1Offset/Task1Offset/CountSort.h @@ -0,0 +1,4 @@ +#pragma once + +// function for sorting an array +int* countSortArray(int* arrayOfNumbers, int numberOfElements); diff --git a/Task1Offset/Task1Offset/Task1Offset.vcxproj b/Task1Offset/Task1Offset/Task1Offset.vcxproj new file mode 100644 index 0000000..5447b01 --- /dev/null +++ b/Task1Offset/Task1Offset/Task1Offset.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {40f6e0d7-3ed6-4169-86f0-26255f01154f} + Task1Offset + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters b/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters new file mode 100644 index 0000000..ac2764c --- /dev/null +++ b/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/Task1Offset/Task1Offset/TestCountSort.c b/Task1Offset/Task1Offset/TestCountSort.c new file mode 100644 index 0000000..91da112 --- /dev/null +++ b/Task1Offset/Task1Offset/TestCountSort.c @@ -0,0 +1,74 @@ +#include "TestCountSort.h" +#include "CountSort.h" +#include + +bool sortingheck(int* arrayOfNumber, int numberOfElements) +{ + for (int i = 0; i < numberOfElements - 1; i++) + { + if (arrayOfNumber[i] > arrayOfNumber[i + 1]) + { + return false; + } + } + return true; +} + +// Function for testing sorting counting +bool correctCountSort() +{ + // Checking the operation of sorts for a random set of numbers + int arrayOfNumbersForSorting[100] = { 0 }; + for (int i = 0; i < 100; i++) + { + arrayOfNumbersForSorting[i] = rand(); + } + int* array = countSortArray(arrayOfNumbersForSorting, 100); + free(array); + // Checking the operation of sorts for an array consisting of equal numbers + int arrayOfEqualElements[40] = { 0 }; + for (int i = 0; i < 40; i++) + { + arrayOfEqualElements[i] = 144; + } + array = countSortArray(arrayOfEqualElements, 40); + free(array); + + // Checking the operation of sorts for a sorted array + int sortedArray[56] = { 0 }; + for (int i = 0; i < 56; i++) + { + sortedArray[i] = i; + } + array = countSortArray(sortedArray, 56); + free(array); + + // Checking the sorting operation for an array consisting of 1 element + int arrayOfOneElement[1] = { 138 }; + array = countSortArray(arrayOfOneElement, 1); + free(array); + + // Checking for an array of 0 elements + int arrayOfZeroElement[1] = { 138 }; + array = countSortArray(arrayOfOneElement, 0); + return sortingheck(arrayOfNumbersForSorting, 100) + && sortingheck(arrayOfEqualElements, 40) + && sortingheck(sortedArray, 56) + && sortingheck(arrayOfOneElement, 1) + && sortingheck(arrayOfZeroElement, 0); +} + +bool checkTheArrayCounter() +{ + int arrayOfNumbers[10] = {1, 4, 1, 1, 6, 8, 9, 12, 123, 78}; + int* arrayCounter = countSortArray(arrayOfNumbers, 10); + bool firstCheck = arrayCounter[1 - 1] == 3; + bool secondCheck = arrayCounter[4 - 1] == 1; + bool thirdCheck = arrayCounter[6 - 1] == 1; + bool fourthCheck = arrayCounter[8 - 1] == 1; + bool fifthCheck = arrayCounter[9 - 1] == 1; + bool sixthCheck = arrayCounter[12 - 1] == 1; + bool seventhCheck = arrayCounter[123 - 1] == 1; + free(arrayCounter); + return firstCheck && secondCheck && thirdCheck && fourthCheck && fifthCheck && sixthCheck && seventhCheck; +} \ No newline at end of file diff --git a/Task1Offset/Task1Offset/TestCountSort.h b/Task1Offset/Task1Offset/TestCountSort.h new file mode 100644 index 0000000..0ad92c8 --- /dev/null +++ b/Task1Offset/Task1Offset/TestCountSort.h @@ -0,0 +1,7 @@ +#pragma once +#include + +// Function for testing sorting counting +bool correctCountSort(); + +bool checkTheArrayCounter(); From d18ca3b671b51b72e744fed25211cc545d9ae274 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 21 Dec 2021 12:48:02 +0300 Subject: [PATCH 2/3] writing basic functions for sorting an array --- Task1Offset/Task1Offset/CountSort.c | 2 +- Task1Offset/Task1Offset/{ASD.C => Task1.c} | 8 +++++--- Task1Offset/Task1Offset/Task1Offset.vcxproj | 2 +- Task1Offset/Task1Offset/Task1Offset.vcxproj.filters | 2 +- Task1Offset/Task1Offset/TestCountSort.h | 1 + 5 files changed, 9 insertions(+), 6 deletions(-) rename Task1Offset/Task1Offset/{ASD.C => Task1.c} (83%) diff --git a/Task1Offset/Task1Offset/CountSort.c b/Task1Offset/Task1Offset/CountSort.c index e2ab41c..cd8b998 100644 --- a/Task1Offset/Task1Offset/CountSort.c +++ b/Task1Offset/Task1Offset/CountSort.c @@ -29,7 +29,7 @@ int* countSortArray(int* arrayOfNumbers, int numberOfElements) int* arrayOfCount = (int*)calloc((maximumElementInArray - minimumElementInArray + 1), sizeof(int)); if (arrayOfCount == NULL) { - return -1; + return NULL; } // For each element, we increase the counter for its value for (int i = 0; i <= numberOfElements - 1; i++) diff --git a/Task1Offset/Task1Offset/ASD.C b/Task1Offset/Task1Offset/Task1.c similarity index 83% rename from Task1Offset/Task1Offset/ASD.C rename to Task1Offset/Task1Offset/Task1.c index 1a80671..b2dc835 100644 --- a/Task1Offset/Task1Offset/ASD.C +++ b/Task1Offset/Task1Offset/Task1.c @@ -19,7 +19,8 @@ int main() while (temporary != 0) { printf("enter the %d number\n", counter); - scanf_s("%d\n", &temporary); + scanf_s("%d", &temporary); + printf("\n"); array[counter] = temporary; if (array[counter] < minimum) { @@ -31,9 +32,10 @@ int main() } counter++; } - int* count = countSortArray(array, 10); + int* count = countSortArray(array, counter); printf("\n"); - for (int i = 0; i < 10; i++) + printf("element - quanity\n"); + for (int i = 0; i < counter; i++) { if (i >= 1 && array[i] == array[i - 1]) { diff --git a/Task1Offset/Task1Offset/Task1Offset.vcxproj b/Task1Offset/Task1Offset/Task1Offset.vcxproj index 5447b01..bb3f069 100644 --- a/Task1Offset/Task1Offset/Task1Offset.vcxproj +++ b/Task1Offset/Task1Offset/Task1Offset.vcxproj @@ -139,7 +139,7 @@ - + diff --git a/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters b/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters index ac2764c..466a12c 100644 --- a/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters +++ b/Task1Offset/Task1Offset/Task1Offset.vcxproj.filters @@ -15,7 +15,7 @@ - + Исходные файлы diff --git a/Task1Offset/Task1Offset/TestCountSort.h b/Task1Offset/Task1Offset/TestCountSort.h index 0ad92c8..b663930 100644 --- a/Task1Offset/Task1Offset/TestCountSort.h +++ b/Task1Offset/Task1Offset/TestCountSort.h @@ -4,4 +4,5 @@ // Function for testing sorting counting bool correctCountSort(); +// Function for checking the element counter bool checkTheArrayCounter(); From 0f3bb7aae2be60b49875ba714cd952cf31b5ceaf Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 21 Dec 2021 12:53:51 +0300 Subject: [PATCH 3/3] fixed bugs --- Task1Offset/Task1Offset/Task1.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Task1Offset/Task1Offset/Task1.c b/Task1Offset/Task1Offset/Task1.c index b2dc835..fc57047 100644 --- a/Task1Offset/Task1Offset/Task1.c +++ b/Task1Offset/Task1Offset/Task1.c @@ -16,6 +16,7 @@ int main() int minimum = INT_MAX; int maximum = INT_MIN; int array[1000] = { 0 }; + // You can enter no more than 1000 numbers while (temporary != 0) { printf("enter the %d number\n", counter); @@ -26,10 +27,6 @@ int main() { minimum = array[counter]; } - if (array[counter] > maximum) - { - maximum = array[counter]; - } counter++; } int* count = countSortArray(array, counter);