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.
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;
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/