Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program In the mathematical theory of sets, a set is defined as a collection

ID: 3825027 • Letter: C

Question

C++ program

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

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
int BinarySearch(int A[], int l, int r, int key)
{
int m;
while( l <= r )
{
m = l + (r-l)/2;
if( A[m] == key )
return m;
if( A[m] < key )
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main(){
   int m=0,n=0,num;
   int temp[100];
   ifstream setA,setB;
   ofstream output;
   setA.open("inputA.dat");
   while(setA>>num){
       temp[m] = num;
       m++;
   }
   int *set1=new int[m];
   for(int i=0;i<m;i++)
       set1[i]=temp[i];
      
   setB.open("inputB.dat");
   while(setB>>num){
       temp[n] = num;
       n++;
   }
      
   int *set2=new int[n];
   for(int i=0;i<n;i++)
       set2[i]=temp[i];
      
  
   output.open("out.dat");
   output<<"Array Elements in File A"<<endl;
   for(int i=0;i<m;i++){
       output<<set1[i]<<" ";
   }
   output<<endl<<endl;
   output<<"Array Elements in File B"<<endl;
   for(int i=0;i<n;i++){
       output<<set2[i]<<" ";
   }
   output<<endl<<endl;
  
   int *unions=new int[m+n];
   int intSize=0;
   if(m>n)
       intSize =m;
   else
       intSize =n;
   int *intersection=new int[intSize];
   sort(set1,set1+m);  
   sort(set2,set2+n);
  
   output<<"Sorted ArrayA"<<endl;
   for(int i=0;i<m;i++){
       output<<set1[i]<<" ";
   }
   output<<endl<<endl;
   output<<"Sorted ArrayB"<<endl;
   for(int i=0;i<n;i++){
       output<<set2[i]<<" ";
   }
   output<<endl<<endl;
  
   int l=0;
   int s=0;
   for(int i=0;i<m;i++){
       if(BinarySearch(set2,0,n-1,set1[i])!=-1){
           intersection[l++]=set1[i];
       }else{
           temp[s++]=set1[i];
       }
   }
   int k=0;
   for(int i=0;i<n;i++)
       unions[k++]=set2[i];
   for(int i=0;i<m;i++){
       if(BinarySearch(set2,0,n-1,set1[i])==-1){
           unions[k++]=set1[i];
       }
   }
   int *diff=new int[s];
   for(int i=0;i<s;i++){
       diff[i]=temp[i];
   }
   output<<"Difference from ArrayA and ArrayB"<<endl;
   for(int i=0;i<s;i++)
       output<<diff[i]<<" ";
   output<<endl<<endl;
   output<<"Number of elements in intersection = "<<l<<endl;
   for(int i=0;i<l;i++)
       output<<intersection[i]<<" ";
   output<<endl<<endl;
   output<<"Number of elements in union = "<<k<<endl;
   for(int i=0;i<k;i++)
       output<<unions[i]<<" ";
   output<<endl<<endl;
   sort(unions,unions+k);
   cout<<"Union of setA and setB : ";
   for(int i=0;i<k;i++){
       cout<<unions[i]<<" ";
   }
   cout<<endl;
   cout<<"diff of setA and setB : ";
   for(int i=0;i<s;i++){
       cout<<diff[i]<<" ";
   }
   cout<<endl;
   sort(intersection,intersection+l);
   output<<"Sorted intersection(no duplicates)"<<endl;
   for(int i=0;i<l;i++)
       output<<intersection[i]<<" ";
   output<<endl<<endl;
   output<<"Sorted union(no duplicates)"<<endl;
   for(int i=0;i<k;i++)
       output<<unions[i]<<" ";
   cout<<"Intersection of setA and setB : ";
   for(int i=0;i<l;i++){
       cout<<intersection[i]<<" ";
   }
   cout<<endl;
   return 0;
}

Result:

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 = 20
-6 -1 0 3 4 5 7 11 12 13 16 21 -11 -3 1 6 8 9 15 17

Sorted intersection(no duplicates)
0 4 5 7 11 12

Sorted union(no duplicates)
-11 -6 -3 -1 0 1 3 4 5 6 7 8 9 11 12 13 15 16 17 21

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote