Write a program that computes the union, intersection, and difference of two set
ID: 3007171 • Letter: W
Question
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 union (int a[], int b[], int asize, int bsize, int union[]);
void sort (int array[], int n);
SAMPLE OUTPUT:
Enter filenames=>inputA.dat
inputB.dat
out.dat
Array Elements in File A
0 1 -3 5 -11 6 8 9 11 17 15 7 4 12
Array Elements in File B
0 -1 3 7 -6 16 5 11 12 4 21 13
Sorted ArrayA
-11 -3 0 1 4 5 6 7 8 9 11 12 15 17
Sorted ArrayB
-6 -1 0 3 4 5 7 11 12 13 16 21
Difference from ArrayA and ArrayB
-11 -3 1 6 8 9 15 17
Number of elements in intersection = 6
0 4 5 7 11 12
Number of elements in union = 26
-6 -1 0 3 4 5 7 11 12 13 16 21 -11 -3 0 1 4 5 6 7 8 9 11 12 15 17
Sorted intersection
0 4 5 7 11 12
Sorted union
-11 -6 -3 -1 0 0 1 3 4 4 5 5 6 7 7 8 9 11 11 12 12 13 15 16 17 21
The Intersection of A and B (no duplicates)
0 4 5 7 11 12
The Union of A and B(no duplicates)
-11 -6 -3 -1 0 1 3 4 5 6 7 8 9 11 12 13 15 16 17 21
Explanation / Answer
#include #include void Union(int set1[10],int set2[10],int m,int n); void Intersection(int set1[10],int set2[10],int m,int n); void main() { int a[10],b[10],m,n,i,j; int ch; clrscr(); printf(" Enter the number of elements in first set: "); scanf("%d",&m); printf(" Enter the elements: "); for(i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.