Help to correctly add the sorting condition from 1 found even number in the array to the last, i.e. -3 5 2 11 19 14 10 9 - sorts from 2 to 10. Language with ++. The sorting method of choice.
void choicesSort(int *mas, const int n) { int min = 0; int buf = 0; for (int i = 0; i < n; i++) { min = i; for (int j = i + 1; j < n; j++) min = (mas[j] < mas[min]) ? j : min; if (i != min) { buf = mas[i]; mas[i] = mas[min]; mas[min] = buf; } } } Full code:
#include <Windows.h> #include <cstdio> #include <conio.h> #include <ctime> #include <cstdlib> void choicesSort(int*, const int); void Init_Array(const int, int*, const int, const int); void Print_Array(const int, const int*); void Work(int*, int); void main() { SetConsoleOutputCP(1251); SetConsoleCP(1251); system("cls"); int const max_size = 30, const left = -15, const right = 20; int my_array[max_size]; Init_Array(max_size, my_array, left, right); printf("Вхідний масив:\n"); Print_Array(max_size, my_array); printf("\n"); choicesSort(my_array, max_size); printf("Вихідний масив:\n"); Print_Array(max_size, my_array); printf("\n"); Work(my_array, max_size); system("pause"); } void Init_Array(const int n, int *mas, const int a, const int b) { srand(time(0)); for (int i = 0;i<n; i++) mas[i] = a + rand() % (b - a+1 ); } void Print_Array(const int n, const int *mas) { for (int i = 0; i < n; i++) printf("%3d", mas[i]); } void choicesSort(int *mas, const int n) { int min = 0; int buf = 0; for (int i = 0; i < n; i++) { min = i; for (int j = i + 1; j < n; j++) min = (mas[j] < mas[min]) ? j : min; if (i != min) { buf = mas[i]; mas[i] = mas[min]; mas[min] = buf; } } } void Work(int *mas, int n) { int i; int alph = 0; float persent; for (int i = 0; i < n; i++) { if (mas[i] > 0) alph++; } persent = (alph / 30.) * 100.; printf("Відсоток чисел які мають додатнє значення: %5.2f %%\n", persent); }