removing duplicates from union, intersection, and difference functions

Oct 7, 2010 at 4:56pm
I am almost finished with this programming I am working on but I am having one problem with a segment of my code. I can not get rid of the duplicates for the functions for union and intersection. I was able to get rid of the duplicates for the arrays a and b but I can not get the duplicates out of the Union function even though I think I set it up right. I think I know my error but I am not sure how to fix it. My Union function is not reading in the correct size for setF(array set a without duplicates) and setG(array set b without the duplicates). Can anyone help me fix my code? I have been working on it for days and I still can not find a way to remove the duplicates correctly, If you can suggest another way of removing the duplicates that would also be very helpful. Thanks.

Here is the txt file:
1 2 3 4 5 6 7 8 9 10 5 6 7 4 65 1 2 5 4 3
5 5 5 5 6 7 3 4 6 5 11 2 43 55 65 1 5 4 2 3
1 1 2 4 3 5 6 8 9 10 99 65 20 15 4 6 7 18 92 21

Each line has 20 integers. First 10 integers are array a and second 10 integers are array b. I then have to store them in an array (program requirements).


Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
void compute_union (int a[10], int b[10], int u[20]);
void compute_intersection(int a[10], int b[10], int x[10]);
void compute_differences(int a[10], int b[10], int d[10]);
void filterSets(int u[], int setI[]);

int main()
{
ifstream infile;
int a[10];
int b[10];
int u[20];
int x[10];
int d[10];
int setI[20];
int counter=0;

infile.open("sets.txt");


while(!infile.eof())
{
while(counter < 20)
{
if(counter < 10)
{
infile >> a[counter];
}
else
{
infile >>b[counter-10];
}
counter++;
}
counter = 0;
for(int i = 0; i < 20; i++)
{
if(i < 10)
{
cout<<"Set A contains : "<<a[i]<<endl;
}
else
{
cout<<"Set B contains : "<<b[i-10]<<endl;
}
}
int setF[10];
int countItems = 0;
int numNotDuplicates = 0;

for(int i = 0; i < 10; i++)
{
if(i == 0)
{
setF[countItems] = a[i];
countItems++;
}
else
{
for(int j = 0; j < countItems; j++) //in setF
{
if(a[i] == setF[j])
{
break;
}
else
{
numNotDuplicates++;
}
}//end for j

if(countItems == numNotDuplicates)
{
setF[countItems] = a[i];
countItems++;
}//end if
}//end else

numNotDuplicates = 0;
} //outer for

cout<<"A= { ";
for(int i = 0; i < countItems; i++)
{
cout<<setF[i]<<endl;
}//end for i
cout<<"}"<<endl;

int setG[10];
int countItemstwo = 0;
int numNotDuplicatestwo = 0;

for(int i = 0; i < 10; i++)
{
if(i == 0)
{
setG[countItemstwo] = b[i];
countItemstwo++;
}
else
{
for(int j = 0; j < countItemstwo; j++) //in setF
{
if(b[i] == setG[j])
{
break;
}
else
{
numNotDuplicatestwo++;
}
}//end for j

if(countItemstwo == numNotDuplicatestwo)
{
setG[countItemstwo] = b[i];
countItemstwo++;
}//end if
}//end else

numNotDuplicatestwo = 0;
} //outer for

cout<<"B= { ";
for(int i = 0; i < countItemstwo; i++)
{
cout<<setG[i]<<endl;
}//end for i
cout<<"}"<<endl;


compute_union (a, b,u);
compute_intersection(a, b, x);
compute_differences(a, b,d);
filterSets(u, setI);


}

infile.close();

return 0;
}

void compute_union (int a[10], int b[10], int u[20])
{
int sz_u=0;
int countItemsthree = 0;
int numNotDuplicatesthree = 0;
//compute union
for(int i=0; i<10; i++) {
u[i] = a[i];
sz_u++;
}
int sz_u_old = sz_u;
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<sz_u_old; j++) {
if (b[i] == u[j]) {
found = true;
break;
}
}
if (!found) {
u[sz_u] = b[i];
sz_u++;
}
}
cout<<"union B : ";
for(int i = 0; i < sz_u; i++)
{
cout<<u[i]<<" ";
}//end for i
cout<<"}"<<endl;
}




void compute_intersection(int a[10], int b[10], int x[10])
{

int sz_x=0;
//compute intersection
for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
if (a[i] == b[j]) {
x[sz_x] = a[i];
sz_x++;
}
}
}
cout << "A * B = { ";
for(int i=0; i<sz_x; i++) {
cout << x[i] << " ";
}
cout << "}" << endl;
}
void compute_differences(int a[10], int b[10], int d[10])
{
int sz_d=0;
//compute differences
//A - B
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<10; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
if (!found) {
d[sz_d] = a[i];
sz_d++;
}
}

cout << "A - B = { ";
for(int i=0; i<sz_d; i++) {
cout << d[i] << " ";
}
cout << "}" << endl;
sz_d = 0;
//B - A
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<10; j++) {
if (b[i] == a[j]) {
found = true;
break;
}
}
if (!found) {
d[sz_d] = b[i];
sz_d++;
}
}
cout << "B - A = { ";
for(int i=0; i<sz_d; i++) {
cout << d[i] << " ";
}
cout << "}" << endl;
}
void filterSets(int u[20], int setI[20])
{

int countItemsthree = 0;
int numNotDuplicatesthree = 0;

for(int i = 0; i < 12; i++)
{
if(i == 0)
{
setI[countItemsthree] = u[i];
countItemsthree++;
}
else
{
for(int j = 0; j < countItemsthree; j++) //in setF
{
if(u[i] == setI[j])
{
break;
}
else
{
numNotDuplicatesthree++;
}
}//end for j

if(countItemsthree == numNotDuplicatesthree)
{
setI[countItemsthree] = u[i];
countItemsthree++;
}//end if
}//end else

numNotDuplicatesthree = 0;
} //outer for

cout<<"A union B { ";
for(int i = 0; i < countItemsthree; i++)
{
cout<<setI[i]<<endl;
}//end for i
cout<<"}"<<endl;
}

Last edited on Oct 7, 2010 at 5:04pm
Oct 7, 2010 at 7:46pm
Please post formatted code and within code tags. Use the <> button under the format menu to the side of the box to embed highlighted text within tags.
Oct 7, 2010 at 7:50pm
C++ already provides the algorithms for dealing with sets. Go to this link and look to the left frame of the web page and you'll see the other algorithms related to set theory such as intersection, difference, and symmetric_difference. Why are you reinventing the wheel. For instance the set_union algorithm already removes duplicates because by definition the union of two sets doesn't contain duplicates. The values within the intersection are added to the resultant union only once. I think that if the input sets are carrays with duplicates that the algorithm will ignore the duplicates.
http://cplusplus.com/reference/algorithm/set_union/
Last edited on Oct 7, 2010 at 7:52pm
Topic archived. No new replies allowed.