Short code that i am trying to write. Does a couple things. Makes a 4 by 4 array with the numbers 10, 11, 12, 13. I am supposed to have a function that tells me how many even numbers there are. I don't really know how to call it because the array is multidimensional. Also, I am having difficulty trying to print out my array in a 4 by 4 format.
#include <iostream>
usingnamespace std;
int findEvens(int arg[], int arrsize);
void main ()
{
int ary[4][4] = {0};
int i, j;
//initialize the array
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
ary[i][j] = 10 + i;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
cout << ary[i][j] << endl;
cout << "The number of even values in the array is " << findEvens(ary[i], 16);
system("pause");
}
int findEvens(int arg[], int arrsize)
{
int evencount = 0;
for (int x = 0; x < arrsize; x++)
if (arg[x] % 2 == 0)
evencount++;
return evencount;
}
int findEvens( int arg[][4], int rows )
{
int evencount = 0;
for ( int i = 0; i < rows; i++)
{
for ( j = 0; j < 4; j++ )
{
if ( arg[i] % 2 == 0 ) evencount++;
}
}
return evencount;
}
and call it as findEvens(ary, 4 );
Another way is to define it as
1 2 3 4 5 6 7 8 9 10 11 12 13
int findEvens( int ( &arg )[4][4] )
{
int evencount = 0;
for ( int i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++ )
{
if ( arg[i] % 2 == 0 ) evencount++;
}
}
return evencount;
}
and call it as findEvens(ary );
And the third way is similar the way you are trying to use the function
1 2 3 4 5 6 7 8 9 10 11
int findEvens( int arg[], arrsiize )
{
int evencount = 0;
for (int i = 0; i < arrsize; i++ )
{
if ( arg[i] % 2 == 0 ) evencount++;
}
return evencount;
}
But call it the following way findEvens(&ary[0][0], 16 );
I see. You can probably tell that I am very new to all of this. I really appreciate your help and the time that you are giving up. I wanna ask for one more small favor. I don't really get how to make the array print out in a 4 by 4 block style. I am guessing that I need to utilize the for-loop function somehow but I am not sure.