# Thread: two dimensional array blues.

1. ## two dimensional array blues.

can anyone demonstrate a way to compare a 2d array and check whether each number is different.

Code:
```  int twodarray[4][2] = {
{ 1, 2 },
{ 3, 4 },
{ 5, 6 }
};
if(theyrealldifferent(twodarray)
{
printf("they're all different");
}```

2. cmon brian its only a couple of for loops and an if statement

3. Yeah,
I tried this:
Code:
```int checkthem(int input[4][2])
{
int i = 0;
int j = 0;
int k = 0;
int l = 0;

while(1)
{
if(input[i][j] == input[k][l])
{
return 0;
}
else
{
if(j == 0 && i == 0)
{
j = 1;
}
else if(j == 1 && i == 0)
{
j = 0;
i = 1;
}
else if(i == 1 && j == 0)
{
j = 1;
}
else if(i == 1 && j == 1)
{
i = 0;
j = 0;
if(l == 0 && k == 0)
{
l = 1;
}
else if(l == 1 && k == 0)
{
l = 0;
k++;
}
else if(k == 1 && l == 0)
{
l++;
}
else if(l == 1 && k == 1)
{
return 1;
}
}
}
}
}```
doesn't work.
I suck.

4. ## hmm...

a couple of suggestions...

1) if it ain't mission critical, would it be adviseable to first convert the 2d array into a single dimensional array and, then using just 2 for loops and 1 if-else statement, get the work done?

2) 2d array would involve a plethora of comparisions which could be done using the following code with a couple of modifications/additions:

for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
if(array[i][j]==array[i+1][j]
printf(“Matching nos.!”);
else
{
break;
}
}

I'd rather go route 1....cheers!

5. First off, realise that the problem is really the same as that of a single-dimensional array. A 2d array is just a 1d array where the compiler does some math for you.

This issue is very closely related to sorting, and most of the time, you only worry about having duplicate elements when it's an array you want to sort.

However, if you don't want to sort the array, I suggest using a bubble-sort-like algorithm...
1) Write a function that bubble-sorts the array.

2) Replace the code that looks like this...
Code:
```if (x < y)
{
swap (x, y);
}```
With code that looks like this....
Code:
```if (x == y)
{
return TRUE;
}```