Question: Please help me fix my code. My code is at bottom. Its displaying my files in binary and not with the numbers inputed into the

Please help me fix my code. My code is at bottom. Its displaying my files in binary and not with the numbers inputed into the file.

In the mathematical theory of sets, a set is defined as a collection of distinct items of the same type. In some programming languages, sets are built-in data types; unfortunately, this is not the case in C++. However, we can simulate a set using a one-dimensional array.

Some operations can be performed on sets. We will consider three(3) 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 all elements in both A and B. The intersection of two sets, A and B, is a set that contains elements common to both A and B. 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}, their difference of A and B ( A - 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, intersection or difference.

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[]);

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

#include #include #include

using namespace std;

//Function Prototypes void readfile_array(ifstream& a, ifstream& b, int arraya[], int& asize, int arrayb[], int& bsize); void sortArray (int arr[], int n); void printarray(int arr[], int s, ofstream& o); int unionOperation(int a[], int b[], int asize, int bsize, int unionSet[]); int intersectionOperation(int a[], int b[], int asize, int bsize, int intersectionSet[]); int differenceOperation(int a[], int b[], int asize, int bsize, int differenceSet[]); int duplicates(int setarray[], int d[], int setsize);

//Main Function int main() { int i; string ipfile1, ipfile2, opfile;

ifstream infile1, infile2; ofstream outfile;

int arraya[15], arrayb[15], asize=14, bsize=12; int unionSet[30], unionsize=26, intersectionSet[15], intersectionsize=15, differenceSet[15], differencesize=15; int d[30], duplicatesize;

//Reading file names cout << "Enter File Names: "; cin >> ipfile1 >> ipfile2 >> opfile;

//Opening files for reading infile1.open(ipfile1); infile2.open(ipfile2); outfile.open(opfile);

//Read data from file and store in array readfile_array(infile1, infile2, arraya, asize, arrayb, bsize);

//Sorting array A sortArray(arraya, asize); cout << " Sorted ArrayA: "; outfile << " Sorted ArrayA: "; //Print array printarray(arraya, asize, outfile);

//Sorting array B sortArray(arrayb, bsize); cout << " Sorted ArrayB: "; outfile << " Sorted ArrayB: "; //Print array printarray(arrayb, bsize, outfile);

//Performing difference operation differencesize = differenceOperation(arraya, arrayb, asize, bsize, differenceSet); sortArray(differenceSet, differencesize); cout << " Difference from ArrayA and ArrayB: "; outfile << " Difference from ArrayA and ArrayB: "; printarray(differenceSet, differencesize, outfile);

//Performing intersection operation intersectionsize = intersectionOperation(arraya, arrayb, asize, bsize, intersectionSet); sortArray(intersectionSet, intersectionsize); cout << " Number of elements in intersection = " << intersectionsize << " "; outfile << " Number of elements in intersection = " << intersectionsize << " "; printarray(intersectionSet, intersectionsize, outfile);

//Performing union operation unionsize = unionOperation(arraya, arrayb, asize, bsize, unionSet); sortArray(unionSet, unionsize); cout<<" Number of elements in union = "<< unionsize <<" "; outfile<<" Number of elements in union = "<< unionsize <<" "; printarray(unionSet, unionsize, outfile);

//Performing intersection operation by eliminating duplicates intersectionsize = duplicates(intersectionSet, d ,intersectionsize); sortArray(intersectionSet, intersectionsize); cout << " The Intersection of A and B(no duplicates) "; outfile << " The Intersection of A and B(no duplicates) "; printarray(intersectionSet, intersectionsize, outfile);

//Performing union operation by eliminating duplicates unionsize = duplicates(unionSet, d ,unionsize); sortArray(unionSet, unionsize); cout << " The Union of A and B(no duplicates) "; outfile << " The Union of A and B(no duplicates) "; printarray(unionSet, unionsize, outfile);

cout << " "; return 0; }

//Function that reads data from file void readfile_array(ifstream& a, ifstream& b, int arraya[], int& asize, int arrayb[], int& bsize) {

for(int i = 0; i < asize; i++) a >> arraya[i];

cout << " Array elements in File A: "; for(int i = 0; i < asize; i++) cout << arraya[i] << " ";

for(int i = 0; i < bsize; i++) b >> arrayb[i];

cout << " Array elements in File B: "; for(int i = 0; i < bsize; i++) cout << arrayb[i] << " "; }

//Function that sorts the array void sortArray (int arr[], int n) { int i, j, temp;

for(i=0;i

//Print array void printarray(int arr[], int s, ofstream& o) { int i;

for(i = 0; i < s; i++) { cout << arr[i] << " "; o << arr[i] << " "; } }

//Union Operation int unionOperation(int a[], int b[], int asize, int bsize, int arrayunion[]) { int i,j, k=0;

for(i = 0; i < asize; i++) { arrayunion[k] = a[i]; k++; }

for(j = 0; j < bsize; j++) { arrayunion[k] = b[j]; k++; }

return asize+bsize; }

//Intersection Operation int intersectionOperation(int a[], int b[], int asize, int bsize, int intersectionSet[]) { int i,j, k=0;

bool found = false;

for(i = 0; i < asize; i++) { found = false; for(j = 0; j < bsize; j++) { if(a[i] == b[j]) { found = true; break; } }

if(found == true) { intersectionSet[k] = a[i]; k++; } }

return k; }

//Difference Operation int differenceOperation(int a[], int b[], int asize, int bsize, int differenceSet[]) { int i,j, k=0;

bool found = false;

for(i = 0; i < asize; i++) { found = false; for(j = 0; j < bsize; j++) { if(a[i]==b[j]) { found = true; break; } }

if(found == false) { differenceSet[k] = a[i]; k++; } }

return k; }

//Removing duplicates int duplicates(int setarray[], int uniq[], int setsize) { int c,d,cnt=0;

for(c=0; c < setsize; c++) //For removing duplicate elements { for(d=0;d

for(c=0; c < cnt; c++) setarray[c] = uniq[c];

return cnt; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!