Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     setlocale(LC_ALL, "RUS");
  8.     int k, count = 0;
  9.  
  10.     cout << "Введите количество членов массива: ";
  11.     cin >> k;
  12.  
  13.     int* arr = new int[k];
  14.     int* temp = new int[k];
  15.  
  16.     for (int i = 0; i < k; i++)
  17.     {
  18.         temp[i] = 0;
  19.         arr[i] = rand() % 15 - 9;
  20.         cout << "\nA[" << i << "]: " << arr[i];
  21.     }
  22.  
  23.     for (int i = 0; i < k; i++)
  24.     {
  25.         if (arr[i] < 0)
  26.             count++;
  27.     }
  28.  
  29.     int j = 0, n = k - (k - count);
  30.     for (int i = 0; i < k; i++)
  31.     {
  32.         if (arr[i] < 0) {
  33.             temp[j] = arr[i];
  34.             j++;
  35.         }
  36.         else {
  37.             temp[n] = arr[i];
  38.             n++;
  39.         }
  40.     }
  41.  
  42.     arr = temp;
  43.     cout << endl;
  44.     for (int i = 0; i < k; i++)
  45.     {
  46.         cout << "\t" << arr[i];
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement