The task: to arrange a static array (filled with random numbers) so that even and odd elements alternate in it, the difference is written to the end of the array. Functions do not use.

const int n = 10; int ar[n], ch[n], c2 = 0, c = 0, nch[n], k = 0, p = 0; for (int i = 0; i < n; i++) { ar[i] = rand() % 11; cout << ar[i] << "\t"; } cout << endl; for (int i = 0; i < n; i++) { if (ar[i] % 2 == 0) // если четное присваиваем массиву ch { ch[c] = ar[i]; c++; } else { nch[c2] = ar[i]; // если нечетное массиву nch c2++; } } if (c >= c2) // Если в нечетном цифр больше или поровну { for (int i = 0, j = 1; i < c2 * 2, j < c2 * 2; i += 2, j += 2) // похоже где-то cдесь что-то не то { ar[i] = ch[k]; ar[j] = nch[p]; k++; p++; } for (int i = c2; i < c; i++) // дописываем разницу { ar[i] = ch[i]; } for (int j = 0; j < n; j++) // вывод массива { cout << ar[j] << "\t"; } cout << endl << endl; k = 0; p = 0; } if (c2 > c) { for (int i = 0, j = 1; i < c * 2, j < c * 2; i += 2, j += 2) { ar[i] = ch[k]; ar[j] = nch[p]; k++; p++; } for (int i = c; i < c2; i++) { ar[i] = nch[i]; } for (int j = 0; j < n; j++) { cout << ar[j] << "\t"; } cout << endl << endl; } 
  • Did you run the program? Works correctly? - skegg
  • The numbers are confused - sasha777
  • @ sasha777, actually one auxiliary array is enough (the size of the original one). Even from the initial one enter into its beginning, and odd from the end (i.e. towards each other). Something like: for (i = 0, odd = 0, even = n-1; i <n; i ++) if (ar [i]% 2) aux [odd ++] = ar [i]; else aux [even--] = ar [i]; In the second pass, transfer them to adjacent elements of the original array. Try it, it's simple. - avp
  • Thank. I'm going to try now. - sasha777

1 answer 1

A bit of a strange task, of course - you cannot use functions, but somehow you need to get a random number ...

 #include <cstdlib> #include <iostream> #include <list> #include <vector> int main() { using std::cout; using std::endl; using std::list; using std::vector; const int size=10; int arr[size]; for(int i=0; i<10; i++) { arr[i]=rand()%20; // if(rand()%2) arr[i]*=-1; cout<<arr[i]<<endl; } //чет и нечет list<int> list_even; list<int> list_odd; for(int i=0; i<size; ++i){ if(arr[i]%2) //есть остаток list_even.push_back(arr[i]); else list_odd.push_back(arr[i]); } // cout<<"Merging: "<<endl; //merge sort //alternating numbers vector<int> result_mass; bool flag=true; while(!list_even.empty()||!list_odd.empty()) { if(flag){ if(list_even.empty()) { result_mass.push_back(list_odd.front()); list_odd.pop_front(); } else{ result_mass.push_back(list_even.front()); list_even.pop_front(); } flag=false; } else { if(list_odd.empty()) { result_mass.push_back(list_even.front()); list_even.pop_front(); } else{ result_mass.push_back(list_odd.front()); list_odd.pop_front(); } flag=true; } } for(vector<int>::const_iterator i=result_mass.begin(); i!=result_mass.end(); i++) { cout<<*i<<endl; } return 0; } 

Shl. To connect two arrays with even and odd numbers, I would use merge sort. Read about the stack, data structures, and merge sorting on Wikipedia.

  • Cannot use functions to solve (not yet passed). And how is merge sorting? - sasha777
  • Do you know what a stack is? - PaulD
  • What do you need to write on, in C or C ++? If there is a memory limit, the algorithm will become more complicated. - PaulD 5:53 pm
  • Maybe you need to write some kind of fuzzy bubble sorting algorithm that pushes alternately even odd elements into the desired position in the array ... - PaulD
  • Check with Tichera what is required of you, ask if there are any restrictions on memory. - PaulD 5:57 pm