c++ program set operations of 2 arrays with no duplicates

Hi,

My name is Matt and this is my second year learning c++ programming. I am almost finished with this program 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;
}

If you want you can try running this code on your compiler and see the output to get a clear understanding of what I mean.
Topic archived. No new replies allowed.