Finding common elements between arrays and storing them

• (3 Pages)
• 1
• 2
• 3

39 Replies - 1091 Views - Last Post: 03 February 2013 - 09:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308240&amp;s=c15a78487c57046d1c5437c5213b66eb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 pablomirandavaliente

Reputation: 0
• Posts: 17
• Joined: 20-January 13

Finding common elements between arrays and storing them

Posted 20 January 2013 - 04:04 PM

Hello i'm a c++ beginner and i'm stuck at this, so long i've been using this code
``` 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 < sizeA3; j++)
{
if(array1[i]==array3[j])
newarray2[m]=array1[i];

}
}

```
I don't know where the miskate is, the memo displays 0, but it should display de common element between two arrays (i know there is one because i have the arrays displayed in another 2 memos)

This post has been edited by jimblumberg: 20 January 2013 - 05:16 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Is This A Good Question/Topic? 0

Replies To: Finding common elements between arrays and storing them

#2 mojo666

Reputation: 233
• Posts: 549
• Joined: 27-June 09

Re: Finding common elements between arrays and storing them

Posted 20 January 2013 - 05:05 PM

Try printing the sizeA variables. I'm pretty sure in the given code, they are all set to 1. Also, how is the value of m being handled?

#3 jjl

• Engineer

Reputation: 869
• Posts: 3,994
• Joined: 09-June 09

Re: Finding common elements between arrays and storing them

Posted 20 January 2013 - 07:22 PM

``` int sizeA1=sizeof(array1[3])/sizeof(array1[0]);

```

This value will always be one, since the size of all elements in the array are the same. What you want is the total size of the array in bytes, divided by the size of one element in bytes.

i.e.
```int sizeA1 = sizeof(array1) / sizeof(array1[0]);

```

#4 pablomirandavaliente

Reputation: 0
• Posts: 17
• Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 22 January 2013 - 11:32 AM

So i tried this now, but it didn't work.When i compare the 3 arrays to find common elements, i got a bunch of 0s displayed in the memo and a bunch of 344, i got the 3 arrays displayed in another memo and there aren't any common elements, so it shouldnīt display 344

```int sizearray1=sizeof(array1)/sizeof(array1[0]);
int sizearray2=sizeof(array2)/sizeof(array2[0]);
int sizearray3=sizeof(array3)/sizeof(array3[0]);

for (int m = 0; m < 10; m++)
{
for (int i=0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray2; j++)
{
for (int k = 0; k <sizearray3; k++)
{
if(array1[i]==array2[j]==array3[k])
newarray[m]=array1[i];

}
}
}
}
```

When i try to do the same between arrays 1 and 3 it displays only a bunch of 0, and it should display the common element (the same problem that i posted recently). Im really stuck at this i canīt figure out the problem

#5 jimblumberg

Reputation: 3048
• Posts: 9,282
• Joined: 25-December 09

Re: Finding common elements between arrays and storing them

Posted 22 January 2013 - 11:37 AM

Please post the smallest complete program that illustrates your problem. Also post a small sample of your input files.

Jim

#6 #define

• Duke of Err

Reputation: 977
• Posts: 3,393
• Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 22 January 2013 - 03:43 PM

Here, if array2[j]==array3[k] say is true.

```14    if(array1[i]==array2[j]==array3[k])

```

then it would be equivalent to

```14   if(array1[i]== true)

```

unlikely to be what you want.

Here the Memo is updated every iteration of the loop.

```09    if(array1[i]==array3[j])
10      newarray2[m]=array1[i];
IntToStr(newarray2[m]) );

```

You are probably wanting to update the Memo only when the integers are equal.

```  if(array1[i]==array3[j])
{
newarray2[m]=array1[i];
}

```

You are checking the arrays 10 times here :

```06    for (int m = 0; m < 10; m++)
07    {

```

probably what you wish to do is - when an matching integer is found store the value and increment the index.

Do you need to store the value?

This post has been edited by #define: 22 January 2013 - 03:49 PM

#7 pablomirandavaliente

Reputation: 0
• Posts: 17
• Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 08:23 AM

I have 3 arrays displayed in 1 memo

```int array1[3],array2[3],array3[3];
```

The data of these arrays is the following.

array1=86 172 258 344
array2=57 114 172 229
array3=43 86 129 172

I need to:
1) compare and find any common element between the 3 arrays, and store that element in another array.
2)Also i need to do the same between 2 of them( 1 and 2, 1 and 3, 2 and 3

For 1 i wrote this:

```int sizearray1=sizeof(array1)/sizeof(array1[0]);
int sizearray2=sizeof(array2)/sizeof(array2[0]);
int sizearray3=sizeof(array3)/sizeof(array3[0]);

for (int m = 1; m <= 1; m++)
{
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray2; j++)
{
for (int k = 0; k <sizearray3; k++)
{
if(array1[i]==array2[j]==array3[k])
{
newarray[m]=array1[i];
}
}
}
}
}

```

For the above, the memo3 displays this:
0 0 344 344 344 344 344 344 0 0

In these case it should display 172, since it's the only common element between the 3 arrays

For 2 i have a similar code, comparing array1 and array3:

```for (int m = 1; m <= 1; m++)
{
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray3; j++)
{
if(array1[i]==array3[j])
{
newarray2[m]=array1[i];
}
}
}
}
```

For the above memo4 displays:
0 0 0 0

It should display 86. I think i'm comparing the arrays in a wrong way, but i canīt figure out other way to do this. Please help!

#8 mojo666

Reputation: 233
• Posts: 549
• Joined: 27-June 09

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 10:27 AM

You don't need a for-loop for m. This is just repeating the exact same code. You should initialize m to zero before the loops and increment it whenever you add an element to the array. I don't know why you are getting 0's in the 2 array comparison. Instead of going through the memo, can you just print array1[i] for now, and see if it produces the correct results?

```if(array1[i]==array3[j])
{
cout<<array1[i]<<endl;
}

```

To fix the comparison between the 3 arrays, you want to do the following.

```if(array1[i]==array2[j] && array2[j]==array3[k])
```

This tests if array1[i] is equal to array2[j] and if array2[j] is equal to array3[k]. Your syntax was not valid because compilers will evaluate one operation at a time. So, as #define pointed out it will evaluate one of the comparisons to true/false and then compare true/false to the third number. With the corrected syntax, it will evaluate both comparisons first, then aply an AND (&&) operation to the two results. If they are both true, then we have a common element.

#9 pablomirandavaliente

Reputation: 0
• Posts: 17
• Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 09:56 PM

So mojo666, it must be something like these when comparing the 3 arrays between them

```          m=0;
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray2; j++)
{
for (int k = 0; k <sizearray3; k++)
{
if(array1[i]==array2[j] && array2[j]==array3[k])
{
newarray1[m]=array1[i];
m++;
}
}
}
}
```

These only displays 0s

When i try to compare every combination of the 3 arrays i use this

```            m=0;
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray2; j++)
{
if(array1[i]==array2[j])
{
newarray2[m]=array1[i];
m++;
}
}
}

m=0;
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray3; j++)
{
if(array1[i]==array3[j])
{
newarray3[m]=array1[i];
m++;
}
}
}

m=0;
for (int i=0; i < sizearray2; i++)
{
for (int j = 0; j < sizearray3; j++)
{
if(array2[i]==array3[j])
{
newarray4[m]=array2[i];
m++;
}
}
}
```

But it displays only 0s too. I still think that i'm comparing the arrays in a wrong way.Help please!!

#10 #define

• Duke of Err

Reputation: 977
• Posts: 3,393
• Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 10:27 PM

You are updating newarray1 but reading newarray.

```10       newarray1[m]=array1[i];

```

#11 pablomirandavaliente

Reputation: 0
• Posts: 17
• Joined: 20-January 13

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 10:51 PM

Sorry i'm translating the code to english and made a mistake

```m=0;
for (int i = 0; i < sizearray1; i++)
{
for (int j = 0; j < sizearray2; j++)
{
for (int k = 0; k <sizearray3; k++)
{
if(array1[i]==array2[j] && array2[j]==array3[k])
{
newarray1[m]=array1[i];
m++;
}
}
}
}

```

#12 jjl

• Engineer

Reputation: 869
• Posts: 3,994
• Joined: 09-June 09

Re: Finding common elements between arrays and storing them

Posted 23 January 2013 - 11:33 PM

Any time you find your self using more than two nested loops, then there is probably a better approach to the problem.

Create a function that will intersect two arrays and creates an array that contains the common elements. You can then intersect that common array with your last array to find the intersection of all three arrays.

A intersect B intersect C = (A intersect B ) intersect C

This post has been edited by jjl: 23 January 2013 - 11:33 PM

#13 #define

• Duke of Err

Reputation: 977
• Posts: 3,393
• Joined: 19-February 09

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 01:07 AM

Code seems to work ok.

```void __fastcall TForm1::Button1Click(TObject *Sender)
{
int array1[] = {86, 172, 258, 344};
int array2[] = {57, 114, 172, 229, 86};
int array3[] = {43,  86, 129, 172};

int SIZE_1 = sizeof(array1)/sizeof(array1[0]);
int SIZE_2 = sizeof(array2)/sizeof(array2[0]);
int SIZE_3 = sizeof(array3)/sizeof(array3[0]);

for(int i=0; i<SIZE_1 ;i++)
{
for(int j=0; j<SIZE_2 ;j++)
{
for(int k=0; k<SIZE_3 ;k++)
{
if( array1[i] == array2[j] && array1[i] == array3[k] )
}
}
}

// With data storage array

int data[10];
int m = 0;

for(int i=0; i<SIZE_1 ;i++)
{
for(int j=0; j<SIZE_2 ;j++)
{
for(int k=0; k<SIZE_3 ;k++)
{
if( array1[i] == array2[j] && array1[i] == array3[k] )
{
data[m] = array1[i];
m++;
}
}
}
}

}

```

#14 jjl

• Engineer

Reputation: 869
• Posts: 3,994
• Joined: 09-June 09

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 01:43 AM

I'm going to remphasize the use of functions for a task like this.

example

```#include <iostream>
#include <vector>

template<typename T>
std::vector<T> intersect(const std::vector<T> &veca, const std::vector<T> &vecb) {
std::vector<T> common;

for(int i=0; i < veca.size(); i++) {
for(int j=0; j < vecb.size(); j++) {
if(veca.at(i) == vecb.at(j)) {
common.push_back(veca.at(i));
break;
}
}
}
return common;
}

int main() {
int arrA[5] = {1,2,3,4,5};
int arrB[3] = {4,5,6};
int arrC[6] = {3,4,5,6,7,8};

std::vector<int> vecA(arrA, arrA + 5);
std::vector<int> vecB(arrB, arrB + 3);
std::vector<int> vecC(arrC, arrC + 6);

std::vector<int> commonAB = intersect(vecA, vecB);
std::vector<int> commonABC = intersect(commonAB, vecC);

for(int i=0; i<commonABC.size(); i++)
std::cout<<commonABC.at(i)<<" ";

std::cin.ignore();
std::cin.get();
return 0;
}

```

This post has been edited by jjl: 24 January 2013 - 01:43 AM

#15 baavgai

• Dreaming Coder

Reputation: 4883
• Posts: 11,279
• Joined: 16-October 07

Re: Finding common elements between arrays and storing them

Posted 24 January 2013 - 05:22 AM

pablomirandavaliente, on 23 January 2013 - 10:23 AM, said:

I need to:
1) compare and find any common element between the 3 arrays, and store that element in another array.
2)Also i need to do the same between 2 of them( 1 and 2, 1 and 3, 2 and 3

Use functions! It really makes more sense if you reverse those two requirements.

e.g
```// find common values between two arrays and store the results in a third
void compareArrays(int *a1, int a1Size, int *a2, int a2Size, int *result, int &resultSize);

// display results, using your UI of choice
void showResult(int *result, int resultSize);
void showResult(const char *result);

void compareAllArrays(int *a1, int a1Size, int *a2, int a2Size, int *a3, int a3Size) {
int buffer[MAX_BUFF], bufferSize;

showResult("Compare a1 to a2");
compareArrays(a1, a1Size, a2, a2Size, buffer, bufferSize);
showResult(buffer, bufferSize);

showResult("Compare a1 to a3");
compareArrays(a1, a1Size, a3, a3Size, buffer, bufferSize);
showResult(buffer, bufferSize);

showResult("Compare a2 to a3");
compareArrays(a2, a2Size, a3, a3Size, buffer, bufferSize);
showResult(buffer, bufferSize);

// finding common between all three
showResult("Compare a1 to a2 to a3");
// you already have a2 and a3 in the buffer
// you just need another buffer to compare that result to a1
int buffer2[MAX_BUFF], buffer2Size;
compareArrays(a1, a1Size, buffer, bufferSize, buffer2, buffer2Size);
showResult(buffer2, buffer2Size);
}

```

Hope this helps.

pablomirandavaliente, on 23 January 2013 - 10:23 AM, said:

I need to:
1) compare and find any common element between the 3 arrays, and store that element in another array.
2)Also i need to do the same between 2 of them( 1 and 2, 1 and 3, 2 and 3

Use functions! It really makes more sense if you reverse those two requirements.

e.g
```// find common values between two arrays and store the results in a third
void compareArrays(int *a1, int a1Size, int *a2, int a2Size, int *result, int &resultSize);

// display results, using your UI of choice
void showResult(int *result, int resultSize);
void showResult(const char *result);

void compareAllArrays(int *a1, int a1Size, int *a2, int a2Size, int *a3, int a3Size) {
int buffer[MAX_BUFF], bufferSize;

showResult("Compare a1 to a2");
compareArrays(a1, a1Size, a2, a2Size, buffer, bufferSize);
showResult(buffer, bufferSize);

showResult("Compare a1 to a3");
compareArrays(a1, a1Size, a3, a3Size, buffer, bufferSize);
showResult(buffer, bufferSize);

showResult("Compare a2 to a3");
compareArrays(a2, a2Size, a3, a3Size, buffer, bufferSize);
showResult(buffer, bufferSize);

// finding common between all three
showResult("Compare a1 to a2 to a3");
// you already have a2 and a3 in the buffer
// you just need another buffer to compare that result to a1
int buffer2[MAX_BUFF], buffer2Size;
compareArrays(a1, a1Size, buffer, bufferSize, buffer2, buffer2Size);
showResult(buffer2, buffer2Size);
}

```

Hope this helps.