So, I have to create methods that takes the union and intersection of sets. I must also create a method that removes an element from the set. The union works fine, however, when I call the intersection method in the main, it returns all the elements as 0. For example, I typed in a set {1,2} and another {2,3,4}, so the intersection would be {2}. But, this displays 0. and displays the correct number of elements. So I am unsure what I am doing wrong. Also, the method that removes that set, has a problem with the delete that I have written, so I was wondering how to fix it. Any suggestions on how to fix it?
**side note: I am fully aware of my variable names, I was creating these names to get a better understanding of what I was doing.
//default constructor
Set::Set ( int s ){
if ( s > 0 )
psize = s;
else
psize = DEFAULTSIZE;
//allocate an array of specified size
set = newint[ psize ];
if(!set) {
//send an error is system cannot allocate memory
cout << "Cannot Allocate Memory, exiting program... " << endl;
exit (1);
}
for ( int i = 0; i < psize; i++){
set[i] = 0;
numOfElements = 0;
}
}
bool Set::element ( int n ){
for ( int i = 0; i < psize; i++){
if ( set[i] == n )
returntrue;
}
returnfalse;
}
Set Set::Union( Set &B ){
int newsize = B.numOfElements + numOfElements;
Set C(newsize);
for (int i = 0; i < numOfElements; i++){
C.set[i] = set[i];
}
int indx = 0;
for(int i = 0; i < B.numOfElements; i ++){
if(C.element(B.set[i])){
newsize--;
continue;
}
else
{
C.set[indx + numOfElements] = B.set[i];
indx++;
}
}
C.numOfElements = newsize;
C.display();
return (C);
}
Set Set::Intersection( Set &B ) {
int newsize = numOfElements;
Set C(newsize);
for ( int i = 0; i < numOfElements; i++ ){
if( element(B.set[i]))
C.set[i] = B.set[i];
else{
newsize--;
continue;
}
}
return (C);
}
Set Set::operator-( int n ){
for ( int i = 0; i < numOfElements; i++){
if(element(n)){
delete set[i];
numOfElements--;
}
}
psize = numOfElements;
return (*this);
}
main (){
Set A, B, C;
A.input();
A.display();
B.input();
B.display();
C = A.Union(B);
C.display();
}
.