Some operations can be performed on sets. We will consider only three of them: union, intersection, and difference. These are binary operations requiring two sets as operands. The union of two sets, A and B, is a set that contains elements that are in A and B. The intersection of two sets, A and B, is a set that contains elements common to both A and B. Finally, the difference of two sets, A and B, is a set that contains only the elements in A but not in B and excluding the common elements in A and B.
For example, if A and B are two sets of integers defined as A = {5, 7, 8, 10} and B = {3, 9, 10}, then their union is the set {3, 5, 7, 8, 9, 10}, their intersection is the set {10}, and the difference of A and B is the set {5, 7, 8}.
Write a program that computes the union, intersection, and difference of two sets stored in two one-dimensional arrays. Populate the arrays from the following input files:
inputA.dat
0 1 -3 5 -11 6 8 9 11 17 15 7 4 12
inputB.dat
0 -1 3 7 -6 16 5 11 12 4 21 13
The output should be displayed on screen and in an output file. Prompt user for file names. No duplicates are allowed in union and intersection.
The following functions must be used:
void readfile_array(ifstream& a, ifstream& b, int arraya[], int& asize, int arrayb[], int& bsize);
void printarray(int array[], int size, ofstream& o);
int diff (int a[], int b[], int dif[], int asize, int bsize);
int duplicates (int array[], int d[], int size);
int intersection (int a[], int b[], int asize, int bsize, int inter[]);
int arrayunion (int a[], int b[], int asize, int bsize, int aunions[]);
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
Those should be "easy".
C := A
FOR b in B
IF A does not contain b
THEN append b to C
FOR a in A
IF B contains a
THEN append a to C
FOR a in A
IF B does not contain a
THEN append a to C
With pre-sorted sets there are additional optimization opportunities. Have fun.