Advertisement
skb50bd

[207lec3]_Stack_w/_Array-HW

May 21st, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int n;
  6.     while (true) {
  7.         cout << "Size of the Stack: ";
  8.         cin >> n;
  9.  
  10.         int a[n];
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             cout << "Enter a[" << i << "]: ";
  14.             cin >> a[i];
  15.         }
  16.  
  17.         for (int i = 0, j = n - 1; i < j;) {
  18.             if (a[i] % 2 == 0) i++;
  19.             if (a[j] % 2 == 0) j--;
  20.             if (a[i] % 2 && a[j] % 2) {
  21.                 swap(a[i], a[j]);
  22.                 i++, j--;
  23.             }
  24.         }
  25.  
  26.         for (int i = 0; i < n; i++)
  27.             cout << a[i] << " ";
  28.         cout << endl;
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement