set calculator

my program is not running and i cant find the problem in it please help. It is a set calculator


#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;
for(i=0;i<size1+size2;i++)
{
if(A[i]!=B[i])
{
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;
}

What is the program supposed to do?

What does it do?
the program find intersection,union and difference of the two sets
This:
1
2
int *A=new int(size1); //creating dynamic array
int *B=new int(size2);

Should be:
1
2
int *A=new int[size1]; //creating dynamic array
int *B=new int[size2];


It would help a great deal if the arrays (sets) were sorted.

You have a number of memory errors that aren't related to the task. These would be solved if you create and use a set class with methods intersection, union and difference.

You could enforce sorting and correct populating of each set. That would make the operations earier to work on.
Topic archived. No new replies allowed.