2d arrays

Hi guys, this is what i got so far. I am having trouble having the function compare element by element and returning either true or false. It is only comparing the first element and not the others. help please?

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

const int NCOLS = 3;
const char EMPTY = ' ';

bool compare(char array1[][NCOLS], char array2[][NCOLS], int nrows);

int main ()
{
	char array1[][NCOLS]= {{'A', 'E', 'I'}, {'O', 'U', 'G'}, {'Q', 'M', 'D'}} ;
	char array2[][NCOLS]= {{'b', 'E', 'I'}, {'O', 'u', 'G'}, {'Q', 'M', 'D'}} ;

	compare(array1, array2, 3);

	system ("pause");
	return 0;
}
bool compare(char array1[][NCOLS], char array2[][NCOLS], int nrows)
{
	bool found = false;
	int row = 0;
	int col = 0;
	nrows = 3;
	for (col; col < nrows; col++)
	{
		for (row; row < nrows ; row++)
			if (array1[0][0] == array2[0][0])
			{
				found = true;
				cout << "Same!" << endl;
			}
			else
			{
				found = false;
				cout << "Not Same!" << endl;
			}
	}
return found;
}
Last edited on
It is only comparing the first element and not the others
if (array1[0][0] == array2[0][0])

Yes. That is what you explicitly do. You are dereferencing the first element of each array. Recap: What is an array index?

The other question is, when should compare return true? Is it when the arrays are identical? When each pair of elements is equal? When not even one pair differs?
I think i got it.. thank you keskiverto, youve been much help for me!..i really appreciate it
Topic archived. No new replies allowed.