Counting evens via pointer
Hi Guys
I am trying one of exercises in my book and its asked me to create
an array, pass it to a function and count the number of even numbers
via a pointer.
It compiles but my result is a garbage value? I dont get why as I
have technically assign the value which is a pointer in main to zero.
And I am sure I have dereferenced it correctly.
Code:
// function prototype
void totalEven ( int[], const int, int* );
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
const int ARRAY_SIZE = 12;
int total = 0;
int data[ ARRAY_SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
totalEven ( data, ARRAY_SIZE, &total );
std::cin.get(); // freeze console output window
return 0; // retu#rn value from int main
}
// function to count total even numbers
// in the passed array
void totalEven ( int dat[], const int SIZE, int *pNum )
{
for ( int k = 0; k < SIZE; k += 2 )
{
*( pNum )++;
}
std::cout << "Total even numbers: " << *pNum
<< std::endl;
}