Hi,

my question is simply but a little bit tricky; let assume that there are two array

array1 = (2, 5, 6, 1, 7) and array2 (3, 2, 6, 8) and after function setUnion new array or use pointer return (2, 5, 6, 1, 7, 3, 8). Below my code is shown my problem is I found

(2, 5, 6, 1, 7, 8, 8) How I fix this problem ? Here my code include main function::

``````#include <iostream>
using namespace std;

void setUnion (const int array1[], const int size1, const int array2[], const int size2, int &newSize, int *&unionArray)
{

newSize = size1+size2;
/** int countDuplicate = 0; */
for (int j = 0; j < size1; ++j)
for (int c = 0; c < size2; ++c)
if (array1[j] == array2[c])
newSize--;

unionArray = new int[newSize];

if (size1 > size2)
{
for (int i = 0; i < size1; ++i)
unionArray[i] = array1[i];

int count = 0;
for (int i = size1; i < newSize; ++i)
for (int j = 0; j < size2; ++j)
for (int c = 0; c < size1; ++c)
if (array1[c] != array2[j])
unionArray[i] = array2[j];

}
else /** size2 > || = size1 */
{
for (int i = 0; i < size2; ++i)
unionArray[i] = array2[i];

for (int i = size2; i < newSize; ++i)
for (int j = 0; j < size2; ++j)
for (int c = 0; c < size1; ++c)
if (array1[c] != array2[j])
unionArray[i] = array1[c];
}
}

int main ()
{
cout<<"TYPE"<<endl;
cout<<endl;
const int size1 = 5;
const int array1[size1] ={2, 5, 6, 1, 7};

const int size2 = 4;
const int array2[size2] = {3, 2, 6, 8};

int newSize;
int* unionArray;
int *pNewSize = &newSize;

setUnion(array1, size1, array2, size2, *pNewSize, unionArray);
cout<<"New size :: "<<newSize<<"or use pointer :: "<<pNewSize<<endl;

for (int i = 0; i < newSize; ++i)
cout<<unionArray[i]<<"\t";
cout<<endl;

return 0;
}``````

pls help me...

here is the problem

``````for (int i = size1; i < newSize; ++i)
for (int j = 0; j < size2; ++j)
for (int c = 0; c < size1; ++c)
if (array1[c] != array2[j])
unionArray[i] = array2[j];``````

this code is not correct as logic and as result so think again !!

I change many times but I can not over this problem how I can think in correct way ?

to create non repeated list from list a(size1),list b(size2)
where each list contains non repeated numbers
1-copy a or b to result
2-iterate through the other list b or a
and if the curr element not in result copy it

thank you for your posting I will post correct result both union and also intersect array again thank you daniweb users...

Now I have converted the union question to intersection my arrays're still same

array1 = (2, 5, 6, 1, 7) ^ array2 = (3, 2, 6, 8) and my intersect array should be
intersectArr = (2, 6) but again I found (6, 6) I can not really understand which part of my code should be changed again ? I found count of Duplicate numbers correctly and allocate rightly on memory probably.

Here my code::

``````void setUnion (const int array1[], const int size1, const int array2[], const int size2, int &newSize, int &countDuplication, int *&unionArray, int *&interSectArray)
{
newSize = size1+size2;
countDuplication=0;
for (int i = 0; i < size1; ++i)
for (int j = 0; j < size2; ++j)
if (array1[i] == array2[j])
{
newSize--;
countDuplication++;
}

unionArray = new int[newSize];
interSectArray = new int[countDuplication];

int count = 0;

int index1 = 0;
int index2 = 0;

while (index1 < size1 && index2 < size2)
{
if (array1[index1] == array2[index2])
{
for (int i = 0; i < countDuplication; ++i)
{
interSectArray[count] = array2[index2];
count++;
}

count++;

}
index1++;
index2++;

/**
else if (array1[index1] < array2[index2]) {
index1++;
}
else {
index2++;
}*/
}``````

first 2 loop I found countDuplicate, how many and using this I create new dynamic intersect array and if elements of array1 and elements of array2 is same I throw that repeated elements into an intersect NEW array I can not found my mistake :S

``````for (int i = 0; i < size1; ++i)
for (int j = 0; j < size2; ++j)
if (array1[i] == array2[j])
{

countDuplication++;
}
int count = 0;
unionArray = new int[newSize];
interSectArray = new int[countDuplication];
for (int i = 0; i < size1; ++i)
for (int j = 0; j < size2; ++j)
if (array1[i] == array2[j])
{
interSectArray[count] = array1[i];
count++;
}``````

It works for intersect ...

the union code :

``````void setUnion (const int array1[], const int size1, const int array2[], const int size2, int &newSize, int *&unionArray)
{

newSize = size1+size2;
/** int countDuplicate = 0; */
for (int j = 0; j < size1; ++j)
{
for (int c = 0; c < size2; ++c)
{
if (array1[j] == array2[c])
{
newSize--;
}
}
}

unionArray = new int[newSize];

//use pointers to make code more readable
int*biggerArr,*smallerArr;
int biggerSize,smallerSize;
if (size1 > size2)
{
biggerArr=array1;
biggerSize=size1;
smallerArr=array2;
smallerSize=size2;
}
else
{
biggerArr=array2;
biggerSize=size2;
smallerArr=array1;
smallerSize=size1;
}
//Copy bigger arr to result
//The Fast Copy Way
int count = biggerSize;
memcpy(unionArray,biggerArr,biggerSize*sizeof(int));
//Or the traditional slow copy way
//int count = 0;
//for (int i = 0; i < biggerSize; ++i,counter++)
//{
//	unionArray[counter] = biggerArr[i];
//}

for (int j = 0; j < smallerSize && counter<newSize; ++j)
{
//check if the curr small arr element doesnot exist in the bigger one
bool newValue=true;
for (int c = 0; c < biggerSize; ++c)
{
if (biggerArr[c] == smallerArr[j])
{
newValue=false;
break;
}
}
//if not exist add to result
if(newValue)
{
unionArray[counter]=smallerArr[j];
counter++;
}
}
}``````

Hey thanks it's a very nice idea we can use bool as a function in if's boolean expression now understand ur code.