the program is to find intersection,union and difference of two sets.
the program take the input correctly but after it crashes with the message that some exe is not working
Code:
#include<iostream>
using namespace std;
void Input(int *A, int*B, int size1, int size2) //input function
{
cout<<"enter the first set"<<endl;
for(int i=0;i<size1;i++){
cin>>A[i]; //input of first set
}
cout<<"enter the second set"<<endl;
for(int j=0;j<size2;j++){
cin>>B[j]; //input of second set
}
}
int *Intersection(int *A, int*B, int size1, int size2) //intersection function
{
int *C=0;
int j;
int i;
for(i=0;i<size1+size2;i++)
{
for(j=0;j<size1+size2;j++)
{
if(A[i]==B[j])
{
C[i]=A[i];
}
}
}
for(int k=0;k<i;k++) //printing values after intersection
{
cout<<C[k]<<endl;
}
return C;
}
int *Union(int *A, int*B, int size1, int size2) //union function
{
int *C=0;
int i;
int j;
for(i=0;i<size1+size2;i++)
{
C[i]=A[i];
}
int h=i+1;
for(j=0;j<size1+size2;j++)
{
if(C[j]!=B[j]){
C[h]=B[j];
h++;
}
}
for(int k=0;k<j;k++) //printing values after union
{
cout<<C[k]<<endl;
}
return C;
}
int *Difference(int *A, int*B, int size1, int size2) //difference function
{
int *C=0;
int i;
int j;
for(i=0;i<size1+size2;i++)
{
for(j=0;j<size1+size2;j++)
{
if(A[i]!=B[j])
{
C[i]=A[i];
}
}
}
for(int k=0;k<i;k++) //printing value after (A-B) difference
{
cout<<C[k]<<endl;
}
return C;
}
int main()
{
int size1=0;
int size2=0;
int a;
int *str;
cin>>size1>>size2;
int *A=new int(size1); //creating dynamic array
int *B=new int(size2);
Input(A,B,size1,size2);
cout<<"For insection press 1"<<endl;
cout<<"For union press 2"<<endl;
cout<<"For difference press 3"<<endl;
cin>>a;
if(a==1)
{
str=Intersection(A,B,size1,size2);
}
if(a==2)
{
str=Union(A,B,size1,size2);
}
if(a==3)
{
str=Difference(A,B,size1,size2);
}
delete[]A;
delete[]B;
return 0;
}