Help with assigning array

How can you assign the intersection array? I tried to do it and all I get was

21 21 21 21 21 21 21 21 21 21

What I wanted was 3 7 14 17 21

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` `````` #include using namespace std; const int SIZE = 10; void getIntersection(int setA[], int setB[], int intersection[], int SIZE); void showIntersection(int intersection[], int SIZE); int main() { int setA[SIZE] = { 2,3,5,7,9,10,14,17,20,21 }; int setB[SIZE] = { 1,3,4,7,8,11,14,16,17,21 }; int intersection[SIZE] = { 0 }; getIntersection(setA, setB, intersection, SIZE); showIntersection(intersection, SIZE); return 0; } void getIntersection(int setA[], int setB[], int intersection[], int SIZE) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (setA[i] == setB[j]) { for (int n = 0; n < SIZE; n++) { intersection[n] = setA[i]; } } } } } void showIntersection(int intersection[], int SIZE) { for (int i = 0; i < SIZE; i++) { cout << intersection[i] << " "; } }``````
Last edited on
Maybe like this:
 ``1234567891011121314151617181920`` ``````int getIntersection(int setA[], int setB[], int intersection[], int SIZE) { int k = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (setA[i] == setB[j]) { if (k < SIZE) { intersection[k] = setA[i]; ++k; } } } } return k; }``````

The return value is how many values belong to the intersection.
Ok, thank you. I got it.