dynamic arrays

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;
int *f ( int a[], int l, int &k )
{
	int j = 0; int *p; p = new int [l];
	for ( int i = 0; i < l; i++ ) if ( a[i] % 2 == 0 )
	p[j++] = a[i];
	k = j;
	return p;
}

void main()
{
	const int 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];
}
Are you allowed to use vectors?
No , I don't know vectors yet.
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.
Thanks
Topic archived. No new replies allowed.