x2311

Untitled

Dec 19th, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int arr1[]{1, -2, 4, -5, 7, 8, 9, -121, 42, -5, 2};
  7.     int arraySize = sizeof(arr1) / sizeof(int);
  8.     int arr2[arraySize];
  9.     //variable to move along the new array
  10.     int j = 0;
  11.     //processing elements with a minus
  12.     for (int i = 0; i < arraySize; i++) {
  13.         if (arr1[i] < 0) {
  14.             arr2[j] = arr1[i];
  15.             j++;
  16.         }
  17.     }
  18.     //processing elements with zero or plus
  19.     for (int i = 0; i < arraySize; i++) {
  20.         if (arr1[i] >= 0) {
  21.             arr2[j] = arr1[i];
  22.             j++;
  23.         }
  24.     }
  25.     //array output
  26.     for (int i = 0; i < arraySize; i++) {
  27.         cout << arr2[i] << " ";
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment