Question

In: Computer Science

THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This...

THIS IS IMPLEMENTED IN C++

The SetInt class (Annex B1) represents a set of integers. This class contains a constructor without parameters which creates an empty set and a constructor which receives as parameters an array of integers as well as its size and which creates a set of integers containing the elements of this array. A test program is given in Annex B2.

The class also contains a destructor and the copy constructor, as well as methods that have the following function:

  • add() allows you to add an integer to the set (if it does not already belong to it);

  • remove() allows you to delete an integer from the set;

  • contains() allows to test the membership of an integer to the set;

  • nbElem() which provides the number of elements of the set;

  • tabElem() which returns an array of dynamically allocated integers containing exactly the elements of the set (if the set is empty this function must return NULL).

  • containsAux() allows you to test whether an integer (first argument) belongs to the set in a given position (second argument).

We impose here the use of an array of integers as data members of the class to contain the elements of the set. At any time, the size of the array will be equal to the number of elements in the set, when the set is empty, no array must be allocated.

Questions :

Define the methods of the SetInt class, including constructors and destructors.

Annex B1

/*SetInt.h*/

#include <cstdlib>

using namespace std;

class SetInt

{

public:

SetInt(): elem(NULL), size(0) {};

SetInt(int [], int);

~ SetInt();

SetInt (const SetInt &); //copy constructor

void add(int);

void remove(int);

bool contains(int);

int nbElem();

int *tabElem();

private:

int *elem;

int size;

bool containsAux (int n, int &)

};

Annex B2

/*File myfile2.cpp*/

#include "SetInt.h"

int main() {

SetInt a; // object creation

while (true)

{

cout << "add an element" << << endl;

cin >> elem;

a.add(elem);

cout << "add an other element" << << endl;

cout << "(Y)es/(N)o :" << << endl;

string chain ;

getline(cin,chain); //reads a sequence of characters ending with an end of line and store it in the chain

//object (end of line not included)

if (chain[0]=='n'||'N')break;

}

return 0;

}


Solutions

Expert Solution

class SetInt{

private:

int *elem;

int size;

bool containsAux (int n, int pos){

    if(pos>size){

        return false;

    }

    if(elem[pos]==n){

        return true;

    }

    return false;

}

public:

SetInt(): elem(NULL), size(0) {};

SetInt(int array[], int size){

    elem = new int(size);

    for (int i=0; i<size;i++){

        elem[i]=array[i];

    }

}

~ SetInt(){

}

SetInt (const SetInt & object){

    elem =object.elem;

    size=object.size;

} //copy constructor

void add(int newElement){

    if(contains(newElement)){

        return;

    }

    int *temp =new int(size+1);

    for (int i=0; i<size;i++){

        temp[i]=elem[i];

    }

    temp[size++]= newElement;

    elem =temp;

    temp=NULL;

    delete(temp);

}

void remove(int newElement){

    if(contains(newElement)){

    int *temp =new int(--size);

    for (int i=0; i<size;i++){

        temp[i]=elem[i];

    }

    elem =temp;

    temp=NULL;

    delete(temp);

    }

}

bool contains(int element){      

        for(int i=0;i<size;i++){

            if(elem[i]==element){

                return true;

            }

        }

        return false;

}

int nbElem(){

    return size;

}

int *tabElem(){

    if(size==0){

        return NULL;

    }

    int *temp =new int (size);

    for(int i=0;i<size;i++){

        temp[i]=elem[i];

    }

    return temp;

}

};


Related Solutions

you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a,...
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a, b, c) Most of these function bodies can be written in only a few lines. Error checking is not required. Test your class using the test.h file. The triple.cpp file is the code unit tested on submission. main.cpp #include #include #include #include "triple.h" #include "test.h" using namespace std; int main() { myTest(); return 0; } triple.h #ifndef TRIPLE_H #define TRIPLE_H #include #include using namespace...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute:...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute: int - meridiem: string + Clock() + Clock(hr: int, min: int, mer: string) + setTime(hr: int, min: int, mer: string): void + setHour(hr: int): void + setMinute(min: int): void + setMeridiem(mer: string): void + getHour(): int + getMinute(): int + getMeridiem(): string + void tick() + string asString() + string asStandard() Implementation Details: • The default constructor should set the clock to midnight (12:00...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct elements of S are added, the following six sums are obtained:5,10, 11,13,14,19. Determine the values of a, b, c, and d. (There are two possibilities. )
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
Suppose that two integers from the set of 8 integers {1, 2, … , 8} are...
Suppose that two integers from the set of 8 integers {1, 2, … , 8} are choosen at random. Find the probability that 5 and 8 are picked. Both numbers match. Sum of the two numbers picked is less than 4. [3+3+4 marks] Suppose that you pick a bit string from the set of all bit strings of length ten. Find the probability that: The bit string has the sum of its digits equal to seven. The bit string has...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just...
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT