diff --git a/lab19/lab19/in5.txt b/lab19/lab19/in5.txt new file mode 100644 index 0000000..eb82f5f --- /dev/null +++ b/lab19/lab19/in5.txt @@ -0,0 +1,2 @@ +10 +13 16 5 7 8 9 11 1 2 4 \ No newline at end of file diff --git a/lab19/lab19/lab19.vcxproj b/lab19/lab19/lab19.vcxproj index a129960..7630137 100644 --- a/lab19/lab19/lab19.vcxproj +++ b/lab19/lab19/lab19.vcxproj @@ -134,8 +134,10 @@ + + diff --git a/lab19/lab19/lab19.vcxproj.filters b/lab19/lab19/lab19.vcxproj.filters index d3cb4df..202f01f 100644 --- a/lab19/lab19/lab19.vcxproj.filters +++ b/lab19/lab19/lab19.vcxproj.filters @@ -38,5 +38,11 @@ Файлы ресурсов + + Файлы ресурсов + + + Файлы ресурсов + \ No newline at end of file diff --git a/lab19/lab19/main.c b/lab19/lab19/main.c index d963a14..1c4931c 100644 --- a/lab19/lab19/main.c +++ b/lab19/lab19/main.c @@ -197,6 +197,96 @@ void task4() { free(pout); } +void swap(int* arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +void bubbleSort(int arr[], int n) { + for (int i = 0; i < n - 1; i++) { + + // Last i elements are already in place, so the loop + // will only num n - i - 1 times + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) + swap(arr, j, j + 1); + } + } +} + +void task5() { + int* pdata; + int len = load(&pdata, "in5.txt"); + + int oddCount = 0; + int evenCount = 0; + + for (int i = 0; i < len; i++) { + if (pdata[i] % 2 == 0) { + evenCount++; + } + else { + oddCount++; + } + } + + if (oddCount == 0 || evenCount == 0) { + puts("No odds or evens, exiting task5()"); + free(pdata); + return; + } + + int* peven = (int*)malloc(sizeof(int) * (evenCount + 2)); + if (peven == NULL) { + puts("Out of memory"); + exit(EXIT_FAILURE); + } + + int* podd = (int*)malloc(sizeof(int) * (oddCount + 2)); + if (podd == NULL) { + puts("Out of memory"); + exit(EXIT_FAILURE); + } + + int ieven = 0, iodd = 0; + for (int i = 0; i < len; i++) { + if (pdata[i] % 2 == 0) { + peven[ieven] = pdata[i]; + ieven++; + } + else { + podd[iodd] = pdata[i]; + iodd++; + } + } + + bubbleSort(peven, evenCount); + bubbleSort(podd, oddCount); + + FILE* file = fopen("out5.txt", "w"); + if (file == NULL) { + puts("Failed to create file"); + exit(EXIT_FAILURE); + } + + fprintf_s(file, "%d\n", len); + + for (int i = 0; i < evenCount; i++) { + fprintf_s(file, "%d ", peven[i]); + } + + for (int i = oddCount - 1; i >= 0; i--) { + fprintf_s(file, "%d ", podd[i]); + } + + fclose(file); + + free(pdata); + free(peven); + free(podd); +} + int main() { //saveRandomArray(); @@ -217,5 +307,7 @@ int main() { //task4(); + task5(); + return 0; } \ No newline at end of file diff --git a/lab19/lab19/out5.txt b/lab19/lab19/out5.txt new file mode 100644 index 0000000..cb279c5 --- /dev/null +++ b/lab19/lab19/out5.txt @@ -0,0 +1,2 @@ +10 +2 4 8 16 13 11 9 7 5 1 \ No newline at end of file