Arrays

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.


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


#include <iostream>
using namespace 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;
}






		
			

	
	







You have several possibilities.

First, you can define your function findEvens as

1
2
3
4
5
6
7
8
9
10
11
12
13
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 );

Last edited on
can you explain to me why the ampersand is used in the way I am trying to use my function, please?
Because you shall pass the address of the first element of the array. & - the operator of taking address.
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.
I have not understood your question. Maybe you meant the following

1
2
3
4
5
6
7
8
for ( i = 0; i < 4; i++ )
{
    for ( j = 0; j < 4; j++ )
   {
      cout << ary[i][j] << ' ';
   }
   std::cout << std::endl;
}
Perfect. You have once again managed to impress me. Thank you very, very much for your help.
Topic archived. No new replies allowed.