Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions KRNumber2/KRNumber2/main.c
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (int i = 0; i < size; ++i) {
for (int i = 0; i < size - 1; ++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;

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо какие-то более цивилизованные имена переменных

int i = 0;

Choose a reason for hiding this comment

The 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;
}