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

Closed due to the fact that off-topic participants 0xdb , Kromster , freim , Enikeyschik , LFC on Feb. 27 at 9:52 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, Kromster, freim, Enikeyschik, LFC
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • ideone.com/Wc04OE - it seems to work. - Qwertiy pm
  • No, it does not work. The task is to ensure that the numbers that are between the first and the last detected even numbers (not done) are sorted out of the generated array (done) by selecting (done). choicesSort (), but it hasn’t been done correctly yet .... - Yaroslav
  • And what is the difficulty then? Find the position of the first even (let it be even_beg) and the last even (even_end) in the array, and then start the normal sorting, only by i = even_beg; i <= even_end; i ++ - Drawn Raccoon
  • the problem is only in implementation - Jaroslav
  • Yeah, got it. Answered. - Qwertiy 10:26 pm

1 answer 1

  1. Find l - the index of the first even element
  2. Find r - the index of the last even element
  3. Call choicesSort(mas+l, r-l+1)