Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include<iostream>
using namespace std;
//method to add element to an array in sorted order, and update size reference variable
//element will be added only if it is not present already
void add_sorted(int* arr, int element, int& size){
int index=size;
//finding proper place to add element
for(int j=0;j<size;j++){
if(element<*(arr+j)){
//found the place to add
index=j;
break;
}else if(element==*(arr+j)){
//duplicate, exiting
return;
}
}
//now shifting all elements starting from index position to one place right
for(int j=size;j>index;j--){
*(arr+j)=*(arr+j-1);
}
//adding element at index pos, and incrementing size
*(arr+index)=element;
size++;
}
//method to find the union of arr1 and arr2, and store in result
//returns the number of elements in result.
int find_union(int* arr1, int size1, int* arr2, int size2, int* result){
int count=0;
//looping through each elements in arr1
for(int i=0;i<size1;i++){
int value=*(arr1+i);
//adding to result array in sorted order, if not present already
add_sorted(result,value,count);
}
//repeating same with arr2 elements
for(int i=0;i<size2;i++){
int value=*(arr2+i);
add_sorted(result,value,count);
}
return count;
}
//method to find the intersection of arr1 and arr2, and store in result
//returns the number of elements in result.
int find_intersection(int* arr1, int size1, int* arr2, int size2, int* result){
int count=0;
//looping through all elements in arr1
for(int i=0;i<size1;i++){
int value=*(arr1+i);
//checking if element is present in other array
for(int j=0;j<size2;j++){
int value2=*(arr2+j);
if(value==value2){
//yes. adding to result array in sorted order
add_sorted(result,value,count);
break;
}
}
}
return count;
}
int main(){
int *arr1, *arr2, *un, *in;
int size1, size2, unSize, inSize;
//reading size for arr1
cout<<"Enter size of array 1: ";
cin>>size1;
//initializing arr1 using dynamic memory
arr1=new int[size1];
//asking and reading values to arr1
cout<<"Enter "<<size1<<" integers: ";
for(int i=0;i<size1;i++){
cin>>*(arr1+i);
}
//repeating the same for arr2
cout<<"Enter size of array 2: ";
cin>>size2;
arr2=new int[size2];
cout<<"Enter "<<size2<<" integers: ";
for(int i=0;i<size2;i++){
cin>>*(arr2+i);
}
//initializing array to store union, with max capacity: size1+size2
un=new int[size1+size2];
//initializing array to store intersection. here, size1 or size2 can be used
in=new int[size1];
//finding union, storing in un, storing number of elements in unSize
unSize=find_union(arr1,size1,arr2,size2,un);
//finding intersection
inSize=find_intersection(arr1,size1,arr2,size2,in);
//displaying union and intersection
cout<<"Union: ";
for(int i=0;i<unSize;i++){
cout<<*(un+i)<<" ";
}
cout<<endl;
cout<<"Intersection: ";
for(int i=0;i<inSize;i++){
cout<<*(in+i)<<" ";
}
cout<<endl;
//deleting all arrays before exiting
delete[] arr1;
delete[] arr2;
delete[] un;
delete[] in;
return 0;
}
/*OUTPUT*/
Enter size of array 1: 10
Enter 10 integers: 10 9 8 8 11 23 2 10 3 3
Enter size of array 2: 5
Enter 5 integers: 1 2 3 4 5
Union: 1 2 3 4 5 8 9 10 11 23
Intersection: 2 3
Get Answers For Free
Most questions answered within 1 hours.