Describe the function that in the new array will receive only positive values ​​of the elements of the original array. Use the pointer engine.

Such code causes a bunch of errors, help to fix or find another way.

int *Create(int n) { int *a = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } return a; } int *Filter(int *a, int &n) { int i, j, c; for (i = 0; i < n; i++) { c += (a[i] < 0); } int *b = new int[c]; for (i = 0, j = 0; i < n; i++) { if (a[i] < 0) b[j++] = a[i] { b[j++] = a[i] n = c; } } return b; } //Вывод массива void Write(int *a, int n) { for (i = 0; i < n; i++) { cout << setw(5) << a[i]; cout << endl; } } int n=10; int *a=Create(n); Write(a,n); int *b=Filter(a,n); Write(b,n); 
  • To help, or write for you? - Igor
  • It is advisable to write - msmv
  • if (a[i] < 0) - only positive ?? - Igor

1 answer 1

 int * onlyPositive(int *m, int size) { int a[size], ai=0; for(int i=0; i<size; i++) { if(m[i]>0) { a[ai]=m[i]; ai++; } return a; } 

but it is not exactly)

  • 2
    a little plus did not put for this phrase - "but it is not exactly" - Igor
  • Even if you close your eyes to what is returned from a function, an automatic array of non-constant size is not C ++. - AnT