count() for two dimensional arrays.

I have come across this reference article on the count function:(http://www.cplusplus.com/reference/algorithm/count/) And while testing it, I could not find how to use the function on multiple dimensional arrays.

This is the way I am testing it, with a one dimensional array:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	
	int myArray[3] = {2, 3, 2};
	int myCount = count(myArray, myArray+3, 2);
	
	cout << "The number 2 appears " << myCount << " times";
	
}


And this is the way I tried to use it on the two dimensional array:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	
	int myArray[3][3] = {{2, 3, 2}, {2, 3, 2}};
	int myCount = count(myArray, myArray+6, 2);
	
	cout << "The number 2 appears " << myCount << " times";

}


The second code gives me an error in the file "stl_algo.h". I assume that file has to do with the <algorithm> library, but I have no idea how to fix it, or if there is other way to check for repetition on a two dimensional array.

Thank you.
myArray have type of int**. When dereferenced (in count()) it becomes int*. You just cannot compare pointer and integer.

What you can do is dereference pointer befor passing it: int myCount = count(*myArray, *(myArray+6), 2); However it is probalbly causes an undefined behavior (it depends on whether standard specifies how multidimension array should be placed in memory)
It surely will not work on dynamically allocated arrays.
So you might want to search for alternative solutions.
What solution would you recommend to check for repeated numbers on a two dimensional array? I have been going crazy over loops that will not work.

For example, I am not sure why this is making a strange output on the console:

1
2
3
4
5
6
7
8
9
10
11
12
13

for (x=0; x<5; x++){
	cout << "\n";
		
	for (y=0; y<5; y++){
		bTable[x][y] = PRINT_CARD(y);  }

for (z=(x-1); z>-1; z--){
				
	while(bTable[z][y] == bTable[x][y]){			
          bTable[x][y]= PRINT_CARD(y);}
}
}


Where:
1
2
int x, y, z;
int bTable[5][5]


And

1
2
3
4
5
6
int PRINT_CARD(int y){		
	
	int answer = ((rand()%15)+1) + (15*y);
	
	return answer;
}
I have found that the last code actually works, my mistake was somewhere else.
Topic archived. No new replies allowed.