wohyperion

Новый массив из отрицательных элементов первого

Aug 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int n;
  6.     cout << "Input the number of array elements: ";
  7.     cin >> n;
  8.     int *arr = new int[n], j = 0;
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> arr[i];
  11.         if (arr[i] < 0) {
  12.             j++;
  13.         }
  14.     }
  15.     int *newArr = new int[j], newJ = 0;
  16.     cout << "New array: ";
  17.     for (int i = 0; i < n; i++) {
  18.         if (arr[i] < 0) {
  19.             newArr[newJ] = arr[i];
  20.             cout << newArr[newJ] << endl;
  21.             newJ++;
  22.         }
  23.     }
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment