-
Notifications
You must be signed in to change notification settings - Fork 0
Всё сделал #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Всё сделал #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <locale.h> | ||
| #include <stdbool.h> | ||
|
|
||
| int scanOne(void); | ||
|
|
||
| void bubbleSort(int arrayEvenNumbered[], int size) { | ||
| if (arrayEvenNumbered == NULL) { | ||
| return; | ||
| } | ||
| for (int i = 0; i < size; ++i) { | ||
| for (int j = i + 1; j < size; ++j) { | ||
| if (arrayEvenNumbered[j] < arrayEvenNumbered[j - 1]) { | ||
| int temp = arrayEvenNumbered[j - 1]; | ||
| arrayEvenNumbered[j - 1] = arrayEvenNumbered[j]; | ||
| arrayEvenNumbered[j] = temp; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void sortEvenNumberedPositions(int* arrayOut, int size) { | ||
| int* arrayEvenNumbered = (int*)calloc(size, sizeof(int)); | ||
| if (arrayEvenNumbered == NULL) { | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо как-то вызывающему сигнализировать, что всё плохо |
||
| } | ||
| int j = 0; | ||
| for (int i = 0; i < size; ++i) { | ||
| if (arrayOut[i] % 2 == 0) { | ||
| arrayEvenNumbered[j] = arrayOut[i]; | ||
| ++j; | ||
| } | ||
| } | ||
| bubbleSort(arrayEvenNumbered, j); | ||
| int k = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо какие-то более цивилизованные имена переменных |
||
| int i = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А это вообще не надо, строчкой ниже она заново объявляется |
||
| for (int i = 0; i < size; ++i) { | ||
| if (arrayOut[i] % 2 == 0) { | ||
| arrayOut[i] = arrayEvenNumbered[k]; | ||
| ++k; | ||
| } | ||
| } | ||
| free(arrayEvenNumbered); | ||
| } | ||
|
|
||
| bool test(void) { | ||
| int arrayOut[5] = { 5, 4, 3, 2, 1 }; | ||
| int arrayNeed[5] = { 5, 2, 3, 4, 1 }; | ||
| sortEvenNumberedPositions(arrayOut, 5); | ||
| for (int i = 0; i < 5; ++i) { | ||
| if (arrayOut[i] != arrayNeed[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main(void) { | ||
| setlocale(LC_ALL, "RUS"); | ||
| if (test()) { | ||
| printf("Tests correct\n"); | ||
| } | ||
| else { | ||
| printf("Mistake...\n"); | ||
| return -1; | ||
| } | ||
| printf("Enter the size of the array\n"); | ||
| int size = scanOne(); | ||
| int* arrayOut = (int*)calloc(size, sizeof(int)); | ||
| if (arrayOut == NULL) { | ||
| return -1; | ||
| } | ||
| printf("Enter the elements\n"); | ||
| for (int i = 0; i < size; ++i) { | ||
| arrayOut[i] = scanOne(); | ||
| } | ||
| sortEvenNumberedPositions(arrayOut, size); | ||
| for (int i = 0; i < size; ++i) { | ||
| printf("%d ", arrayOut[i]); | ||
| } | ||
| free(arrayOut); | ||
| } | ||
|
|
||
| int scanOne(void) { | ||
| int number = 0; | ||
| int checkScanf = scanf("%d", &number); | ||
|
|
||
| while (checkScanf != 1) { | ||
| while (getchar() != '\n') { | ||
| } | ||
|
|
||
| printf("%s", "������... ��������� ������������ ����� \n"); | ||
| checkScanf = scanf("%d", &number); | ||
| } | ||
|
|
||
| return number; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А то последняя итерация цикла будет впустую