How do I pass even numbers of an array into another array.

I'm tasked with writing a function that will identify all the even numbers in an sample array {10,2,9,3,1,98,8] and place them in an array called EvenNumbers. I have to allow the function so that it works with different combinations of numbers in the array not just the numbers in the sample array above.

I'm wondering is there any way to add numbers to an array that could be different every time? How would I extract the even numbers an place them into an array? Also
for the even array size its giving me an error that the expression must have a constant value but when I use const int it still gives me that error.

Here is the full question.

"Using the array of sample values {10,2,9,3,1,98,8}, write a function that will identify all the even numbers in an array and place it in an array called EvenNumbers. The function must work in all cases, not just in the case of the array shown. Assume that the array size is always available through a global constant called MAX"

Here is what I have so far. I've no idea how I will extract the even numbers from a for loop and place them in an array. I also dont know what the "expression must have a constant value is about"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>
void EvenNumber(int Array[], int size);

int main()
{
int array[7] = { 10,2,9,3,1,98,8 };

EvenNumber(array, 7);
}

void EvenNumber(int Array[], int size)
{
	int i;
	int EvenArraySize;
	for (i = 0; i < size; i++)
	{

		if (Array[i] % 2 == 0)
		{
			EvenArraySize++;

		}

	}

	int Even[EvenArraySize];

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

// return number of elements copied into the destination array
// invariant: srce_array != NULL, dest_array is large enough
// note: order of arguments is C++ style: ie. copy srce to dest
size_t copy_even_numbers( const int srce_array[], size_t sz, int dest_array[] )
{
    size_t n = 0 ; // number of elements copied into dest_array

    for( size_t i = 0 ; i < sz ; ++i )
    {
        if( srce_array[i]%2 == 0 ) // even number
        {
            dest_array[n] = srce_array[i] ;
            ++n ;
        }
    }

    return n ;
}

int main()
{
    const int srce[] = { 10, 2, 9, 3, 1, 98, 8 } ;
    const size_t sz = sizeof(srce) / sizeof( srce[0] ) ;

    int dest[sz] = {0} ;
    const size_t dest_sz = copy_even_numbers( srce, sz, dest ) ;

    for( size_t i = 0 ; i < dest_sz ; ++i ) printf( "%d ", dest[i] ) ;
    puts("") ;
}
std::copy_if() might be a 'modern' C++ approach with all its attendant bells and whistles like std::vector, std::back_inserter and, of course, the said algorithm though I see OP has to use C-style arrays:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>
#include <iterator>

   int main()
{
    const int srce[] = { 10, 2, 9, 3, 1, 98, 8 } ;
   
    std::vector<int> dest;
    std::copy_if(srce, srce + sz, std::back_inserter(dest), [](int i){return (i%2) == 0;});
    for (const auto& elem : dest)std::cout << elem << " "; std::cout << "\n";
}


Topic archived. No new replies allowed.