Добавить task4.cpp
This commit is contained in:
34
task4.cpp
Normal file
34
task4.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <iostream>
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user