Advertisement
Sanlover

Untitled

Nov 18th, 2021
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.   int N, min, max;
  6.   cout << "Enter N: ";
  7.   cin >> N;
  8.   if (N <= 0 || N % 2 != 0) {
  9.     cout << "N must be positive and even";
  10.     return 0;
  11.   }
  12.   int* array = new int[N];
  13.   cout << "Fill the array:" << endl;
  14.  
  15.   min = max = N / 2 - 1;
  16.   for (int i = 0; i < N; i++) {
  17.     cout << i + 1 << ") ";
  18.     cin >> array[i];
  19.     if (i < N / 2 - 1) {
  20.       if (array[min] > array[i]) {
  21.         min = i;
  22.       }
  23.     } else {
  24.       if (array[i] > array[max]) {
  25.         max = i;
  26.       }
  27.     }
  28.   }
  29.   int temp = array[max];
  30.   array[max] = array[min];
  31.   array[min] = temp;
  32.  
  33.   cout << endl
  34.        << "Your array afer swapping min from first half to max from second:";
  35.   for (int i = 0; i < N; i++) {
  36.     cout << array[i] << ' ';
  37.   }
  38.   return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement