Hello everyone.
Here my question is. I need to write a code using dynamic arrays. The function must get an array and return its even terms. But my code does not work at all , can anybody explain to me what's wrong with it?
#include <iostream>
usingnamespace std;
int *f ( int a[], int l, int &k )
{
int j = 0; int *p; p = newint [l];
for ( int i = 0; i < l; i++ ) if ( a[i] % 2 == 0 )
p[j++] = a[i];
k = j;
return p;
}
void main()
{
constint n = 6;
int i, a[n], k;
for ( i = 0; i < n; i++ )
cin >> a[i];
cout << f ( a, n, k ) << endl;
cin >> a[i];
}
Line 18: f returns a pointer. Your cout statement is going to output that pointer.
If you want to display the values in the returned array, you're going to have to assign the returned pointer to a local pointer, then iterate through the returned values, outputting each one.