Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Needs to be in C++ Programming; Write a class called IntSet, which repre

ID: 3765780 • Letter: P

Question

Project Needs to be in C++ Programming;

Write a class called IntSet, which represents a mathematical set of integers. It should have the following data members:

a pointer-to-int that will point to a dynamically allocated array that holds the values currently in the IntSet

an int that holds the current size of the array - it will need to be updated whenever the add() method creates a larger array

an int that holds the number of values currently in the IntSet - it will need to be updated in the add() and remove() methods

The IntSet class should have the following methods:

a default constructor that initializes the first data member to point to an array of 10 ints, initializes the size of the array to 10, and initializes the number of values in the set to zero

a destructor that deallocates the dynamically allocated array

the size() method should return the number of values currently in the IntSet

the isEmpty() method should return true if the IntSet contains no integers, and return false otherwise

the contains() method should take an int parameter and return true if that value is in the IntSet, and return false otherwise - you will find this function useful

the add() method should take an int parameter and add that int to the IntSet (if that value is not already in the IntSet) - if the array is currently full and you need to add another int, then you must first increase the size of the array by allocating a new array that is twice as large, copying the contents of the old array into the new array, redirecting the data member pointer to the new array, and deallocating the old array

the remove() method should take an int parameter and remove it from the IntSet (if that value is in the IntSet) by shifting over all of the subsequent elements of the array

the addAll() method: setA.addAll(setB) should add to setA any values from setB that were not already in setA (i.e. setA becomes the union of the two sets) - the parameter should be a const reference

the removeDifferent() method: setA.removeDifferent(setB) should remove from setA any values that are not also in setB (i.e. setA becomes the intersection of the two sets) - the parameter should be a const reference

the removeSame() method: setA.removeSame(setB) should remove from setA any values that are also in setB (i.e. setA becomes the relative complement of setA in setB) - the parameter should be a const reference

Note: When designing a class that has dynamically allocated data members, you would normally overload the copy constructor, the assignment operator, and possibly the equality operator (you'll see these in CS 162). The copy constructor for a class is invoked any time you:

pass an object of that class by value

return an object of that class from a function

declare an object of that type and assign a value to it in the same line (if you

   assign a value on a different line then the assignment operator is invoked)

The files must be named: IntSet.hpp and IntSet.cpp

Explanation / Answer

Implemented first 5 sub parts

/*
* Inset.h
*
* Created on: 25-Nov-2015
*      Author: Ravi
*/

#include <iostream>
#include<stdlib.h>

using namespace std;

#ifndef INSET_H_
#define INSET_H_

class Inset {

private:
   int *data;
   int sizeOfArray;
   int noOfValuees;

public:
   Inset();
   virtual ~Inset();
   void add(int ele);
   void display();
   int size();
   bool isEmpty();
   void remove(int ele);
   bool contains(int ele);


};

#endif /* INSET_H_ */

#include "Inset.h"


int main()
{
   cout << "Node list" <<endl;
   Inset list;
   int i;
   for(i = 0; i < 15; i++) {
       list.add(i+1);
   }

   list.display();
   cout << " Search for 6 exist : " << list.contains(6) <<endl;
   list.remove(6);
   list.display();

}

--------------------output-------------------------

Node list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Search for 6 exist : 1
1 2 3 4 5 7 8 9 10 11 12 13 14 15

/*
* Inset.cpp
*
* Created on: 25-Nov-2015
*      Author: Ravi
*/

#include "Inset.h"

Inset::Inset() {
   // TODO Auto-generated constructor stub
   sizeOfArray = 10;
   data = NULL;
   noOfValuees = 0;

}

Inset::~Inset() {
   // TODO Auto-generated destructor stub
}

void Inset::add(int ele) {
   if(data == NULL || noOfValuees >= sizeOfArray)
       data = (int *)realloc(data, (sizeOfArray+1) * sizeof(int));
   data[noOfValuees++] = ele;
   if(noOfValuees >= sizeOfArray)
       sizeOfArray++;
}

void Inset::display() {
   int i;
   for(i = 0; i < noOfValuees; i++)
       cout << data[i] << " ";
   cout<<endl;
}

int Inset::size() {
   return noOfValuees;
}

bool Inset::isEmpty() {
   return noOfValuees == 0;
}

bool Inset::contains(int ele) {
   int i;
   for(i = 0; i < noOfValuees; i++) {
       if(data[i] == ele)
           return true;
   }
   return false;
}

void Inset::remove(int ele) {
   int i;
   for(i = 0; i < noOfValuees; i++) {
           if(data[i] == ele) {
               break;
           }
   }

   int j;
   for(j = i; j < noOfValuees-1; j++) {
       data[j] = data[j+1];
   }
   noOfValuees--;
}