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 C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return empty queue // enqueue and dequeue each return an int error code int enqueue(int, Queue *) int dequeue(Queue *, int *) int getQsize(Queue *) // returns # of items in queue void freeQueue(Queue *) // Free *all* space Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time. You will need to document each of your functions to...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT