diff --git a/task4.cpp b/task4.cpp new file mode 100644 index 0000000..eab8438 --- /dev/null +++ b/task4.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +int* removeNeg(int* a, int n, int& newN) { + int* b = new int[n]; + int k = 0; + for (int i = 0; i < n; i++) + if (a[i] >= 0) b[k++] = a[i]; + newN = k; + int* c = new int[k]; + for (int i = 0; i < k; i++) c[i] = b[i]; + delete[] b; + return c; +} + +int main() { + srand(time(0)); + int n = 10; + int* a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = rand() % 21 - 10; + cout << a[i] << " "; + } + cout << endl; + int newN; + int* b = removeNeg(a, n, newN); + cout << "New array: "; + for (int i = 0; i < newN; i++) + cout << b[i] << " "; + cout << endl; + delete[] a; + delete[] b; + return 0; +} \ No newline at end of file