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

C++ Coding Create a class called IntegerSet . Each object of class IntegerSet ca

ID: 3834154 • Letter: C

Question

C++ Coding

Create a class called IntegerSet.   Each object of class IntegerSet can hold positive integers in the range 0 through 255 and most common set operations are available. A set is represented internally as a array of true and false. Array element a[ i ] is true if integer i is in the set. Array element a[ j ] is false if integer j is not in the set. The default constructor initializes a set of 256 elements to the so-called “empty set,” i.e., a set whose array representation contains all false values. It also has a constructor that takes an int value m (create a set with m in it) and another constructor that takes an int array as well (create a set with all values in the array). Make sure to reject invalid values that don't belong in a set.

This class shall provide member/friend functions for the common set operations. Provide a union operation that creates a third set which is the set-theoretic union of two existing sets (i.e., an element is in the third set if it is in the first or second set). Provide a intersection operation that creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element is in the third set if it is in both sets). Provide a subtraction operation that creates a third set which is the set-theoretic difference of two existing sets (i.e., an element is in the third set if it is in the first set, but not the second set). Provide an insertElement member function that inserts a new integer k into a set (by setting a[k] to 1). Provide a deleteElement member function that deletes integer m (by setting a[m] to 0). Provide a >> friend function that inputs a set of elements (input one element at a time until -1 is entered). Provide a << friend function that prints a set as a list of numbers separated by comma and one space and enclosed by {}. Print only those elements that are present in the set (i.e., their position in the array has a value of 1; for example, {1, 5, 10}). Print {} for an empty set. Provide both == and != member functions that determine equality of two sets . Provide both subset (<=) and superset (>=) operations (set 1 is a subset of set 2 if all elements in set 1 are also in set 2 and set 2 would be a superset of s1).

In summary, provide public member functions and/or friend functions for each of the following but feel free to add additional private member functions as needed:

Support the following constructors:

IntegerSet();

IntegerSet (int m);

IntegerSet( const int array [], int n);

Union of two IntegerSets (operator +).

Intersection of two IntegerSets (operator *).

Obtain a list of elements in an IntegerSet

Check to see if an element is in an IntegerSent.

Insert an element to an IntegerSet (insertElement).

Remove an element from an IntegerSet (deleteElement).

Input an IntegerSet (stream extraction operator >>).

Output an IntegerSet (stream insertion operator <<).

Comparing two IntegerSets (relational operators ==, !=, <=, >=).

Explanation / Answer

#include<stdio.h>
#include<iostream>
#include<array>
using namespace std;

class IntegerSet
{
private:
          array<bool,256> arr;
public:
         IntegerSet();
         IntegerSet(int m);
         IntegerSet(const int array [], int n);
         IntegerSet operator+(IntegerSet& b);
         IntegerSet operator*(IntegerSet& b);
         IntegerSet operator-(IntegerSet& b);
         void insertElement(int k);
         void deleteElement(int k);
         bool operator!=(IntegerSet& s);
         bool operator==(IntegerSet& s);
         bool operator<=(IntegerSet& s);
         bool operator>=(IntegerSet& s);

         friend istream& operator>>(istream& in,IntegerSet& I)
         {
            int n=0;
            while(1)
            {
              in>>n;
              if(n!=-1)
               I.arr[n]=true;
              else
               break;
            }   
            return in;
         }

         friend ostream& operator<<(ostream& out, const IntegerSet& I)
         {
            for(int i=0;i<256;i++)
            {
               if(I.arr[i]==true)
                out<<i<<" ";
            }
            cout<<endl;
            return out;
         }
       
};

//default constructor initializes all elements of array to false
IntegerSet::IntegerSet()
{
arr={false};
}

//Constructor taking parameter with m and sets true in the arr[m]
IntegerSet::IntegerSet(int m)
{
   arr={false};
   if(m>255)
   {
     cout<<"Out of bounds";
   }
   else
       arr[m]=true;        
}

//build Integer set using an array
IntegerSet::IntegerSet(const int array [], int n)
{
   arr={false};
   for(int i=0;i<n;i++)
   {
     if(array[i]<=255)
       arr[array[i]]=true;
   }
}

//union of two Integer sets
IntegerSet IntegerSet::operator+(IntegerSet& b)
{
     IntegerSet c; //Initially all flase with default constructor
     for(int i=0;i<256;i++)
     {
       if(this->arr[i]==true || b.arr[i]==true) //if present in atleast in one
         c.arr[i]=true;
     }
    return c;
}

//Intersection of two Integer Sets
IntegerSet IntegerSet::operator*(IntegerSet& b)
{
    IntegerSet c; //Initially all flase with default constructor
     for(int i=0;i<256;i++)
     {
       if(this->arr[i]==true && b.arr[i]==true) //If present in both
         c.arr[i]=true;
     }
   return c;
}

//Set difference of two Integer Sets
IntegerSet IntegerSet::operator-(IntegerSet& b)
{
    IntegerSet c; //Initially all flase with default constructor
     for(int i=0;i<256;i++)
     {
       //If present in either of them
       if((this->arr[i]==true && b.arr[i]==false)||(this->arr[i]==false && b.arr[i]==true))
         c.arr[i]=true;
     }
   return c;

}

//inserts a new integer k into a set (by setting arr[k] to 1)
void IntegerSet::insertElement(int k)
{
   if(k>255)
   {
     cout<<"Out of bounds";
   }
   else
       arr[k]=true;
}

//delete an integer k from the set (by setting arr[k] to 0)
void IntegerSet::deleteElement(int k)
{
   if(k>255)
   {
     cout<<"Out of bounds";
   }
   else
       arr[k]=false;
}

//return true if different else false
bool IntegerSet::operator!=(IntegerSet& s)
{
   for(int i=0;i<256;i++)
   {
     if(this->arr[i]!=s.arr[i])
       return true;
   }

return false;
}

//return true if both the sets are same else false
bool IntegerSet::operator==(IntegerSet& s)
{
   for(int i=0;i<256;i++)
   {
     if(this->arr[i]!=s.arr[i])
       return false;
   }

return true;

}

//return true if first set is a subset of second set or else return false
bool IntegerSet::operator<=(IntegerSet& s)
{
   for(int i=0;i<256;i++)
   {
      if(this->arr[i]==true && s.arr[i]!=true)   
       return false;
   }
   return true;
}

//return true if first set is superset of second set or else return false
bool IntegerSet::operator>=(IntegerSet& s)
{
     for(int i=0;i<256;i++)
   {
      if(s.arr[i]==true && this->arr[i]!=true)   
       return false;
   }
   return true;

}