Question

In: Computer Science

USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...

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

Solutions

Expert Solution

#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");
  
}


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT