### 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];
}
}
}
}
just use code as
 ``1234567891011`` `````` for (int i=0; i < sizeof(array1); i++) { for (int j = 0; j < sizeof(array2); j++) { for (int k = 0; k
So now i have this, but it doesn't work either

 ``12345678910111213`` ``````for (int i=0; i < sizeof(array1); i++) { for (int j = 0; j < sizeof(array2); j++) { for (int k = 0; k

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:

 ``12`` ``````int A[4] int size = sizeof(A) / sizeof(A[0]); ``````
So

 ``123456789101112131415161718`` ``````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 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

 ``12345678910`` ``````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??
Topic archived. No new replies allowed.