In: Computer Science
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART
implement the following missing functions from the implementation:
* reset
* intersection
* difference
Set Set::intersection(Set& s){
Set r;
// find intersection
return r;
}
Set Set::difference(Set& s){
Set r;
// find difference
return r;
}
void Set::reset(int c){
// increase the capacity and clear the data
}
driver program
int a1[] = {10,5,7,3,9};
Set s1(5);
s1.insert(a1,5);
s1.print("s1");
int a2[] = {2,9,6};
Set s2(3);
s2.insert(a2,3);
s2.print("s2");
Set s3 = s1.unionset(s2);
Set s4 = s1.intersection(s2);
Set s5 = s1.difference(s2);
s3.print("s3");
s4.print("s4");
s5.print("s5");
N.B
DONT USE VECTORS OR IMPLEMENT ANY OTHER FUNCTION FOR THE CLASS AND OBJECT TYPE THAN THE ABOVE FUNCTION DEFINATION
DONT IMPLEMENT NEW FUNCTIONS GIVE LOGICS FOR THE ABOVE FUNCTION DEFINATION
dont define new functions that s not the part of question
ADD COMMENTS FOR BETTER UNDERSTANDING
#include<bits/stdc++.h>
using namespace std;
class Set
{
public:
int *a;
int size;
void setdata(int
arr[])
{
size=(sizeof(arr)/sizeof(int));
a=(int *)malloc(sizeof(int)*size);
for(i=0;i<size;i++)
{
a[i]=arr[i];
}
}
Set
Set::intersection(Set& s)
{
Set r;
// find intersection
int arr[s.size+5];
int i,j,k;
i=0;
for(j=0;j<s.size;j++)
{
for(k=0;k<size;k++)
{
if(s.a[j]==a[k])
{
arr[i++]=s.a[j];
}
}
}
r.a=arr;
r.size=i;
return r;
}
Set
Set::difference(Set& s)
{
Set r;
// find difference
int arr[s.size+5];
int i,j,k;
i=0;
for(j=0;j<s.size;j++)
{
for(k=0;k<size;k++)
{
if(s.a[j]==a[k])
{
break;
}
}
if(k==size)
{
arr[i++]=s.a[j];
}
}
r.a=arr;
a.size=i;
return r;
}
void Set::reset(int
c)
{
// increase the capacity and clear the data
fill(a.begin(),a.end(),0);
size*=2;
a=(int *)malloc(sizeof(int)*size);
}
};
int main()
{
int a1[] = {10,5,7,3,9};
Set s1(5);
s1.insert(a1,5);
s1.print("s1");
int a2[] = {2,9,6};
Set s2(3);
s2.insert(a2,3);
s2.print("s2");
Set s3 = s1.unionset(s2);
Set s4 = s1.intersection(s2);
Set s5 = s1.difference(s2);
s3.print("s3");
s4.print("s4");
s5.print("s5");
}