comparing 3 arrays and store the common elements in another array

Hi i'm a beginner and i need help with this. The following does not work.Thanks

for (int m=0; m < 10; m++)
{
for (int i=0; i < sizeof(array1); i++)
{
for (int j = 0; j < sizeof(array2); j++)
{
for (int k = 0; k <sizeof(array3); k++)
{
if(array1[i]==array2[j]==array3[k])
newarray[m]=array1[i];
Memo3->Lines->Add(IntToStr(newarray[m]));
}
}
}
}
WRITE your code instead of another outer loop.
just use code as
1
2
3
4
5
6
7
8
9
10
11
 for (int i=0; i < sizeof(array1); i++)
{
        for (int j = 0; j < sizeof(array2); j++)
        {
                        for (int k = 0; k <sizeof(array3); k++)
                        {
                         if(array1[i]==array2[j]==array3[k])
                                  newarray[m]=array1[i];
                          }
         }
}     
So now i have this, but it doesn't work either

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i=0; i < sizeof(array1); i++)
		 {
		  for (int j = 0; j < sizeof(array2); j++)
		   {
			for (int k = 0; k <sizeof(array3); k++)
			 {
			  if(array1[i]==array2[j]==array3[k])
			   newarray[m]=array1[i];


			 }
		   }
		 }

since sizeof returns the size in byte, your for loop only works if the array's datatype is char.
if that's not the case you need to divide it by the size of a single element.
like this:

1
2
int A[4]
int size = sizeof(A) / sizeof(A[0]);  
So

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int sizeA1=sizeof(array1[3])/sizeof(array1[0]);
int sizeA2=sizeof(array2[3])/sizeof(array2[0]);
int sizeA3=sizeof(array3[3])/sizeof(array3[0]);

for (int i=0; i < sizeA1; i++)
		 {
		  for (int j = 0; j < sizeA2; j++)
		   {
			for (int k = 0; k <sizeA3; k++)
			 {
			  if(array1[i]==array2[j]==array3[k])
			   newarray[m]=array1[i];
                           Memo3->Lines->Add(IntToStr(newarray[m]));


			 }
		   }
		 }


Displays 0 in the memo, so it's correct since there are no common elements in the 3 arrays (i have the arrays displayed in other memos, so i know this).
BUT there is 1 common element in array1 and array3 ,and when i write this


1
2
3
4
5
6
7
8
9
10
for (int i=0; i < sizeA1; i++)
		   {
			for (int j = 0; j < sizeA3; j++)
			 {
			   if(array1[i]==array3[j])
				newarray2[m]=array1[i];
				Memo4->Lines->Add(IntToStr(newarray2[m]));

			 }
		   } 


Displays 0, so there is something wrong. Any ideas??
Help please! I'm really stuck
Topic archived. No new replies allowed.