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

Please use C++ to solve : Main : #include <iostream> #include \"IntSet.h\" using

ID: 3689967 • Letter: P

Question

Please use C++ to solve :

Main :

#include <iostream>

#include "IntSet.h"

using namespace std;

int main()
{
    IntSet set1(10);           
    IntSet set2;                
    IntSet set3;               
        IntSet set4;
    int i;

        
        // reading first 5 odd number in set1
    for (i = 1; i < 6; i++)                         
    {                                  
        set1.InsertItem(2*i-1);                 
    }

    cout << endl;                                  
        cout << "set1= ";                 
    cout<<set1;
        cout << endl;
                       

        set2=set1;
        set2.InsertItem(6);
        set2.InsertItem(2);
        cout << "set2= ";                  
    cout<<set2;
        cout << endl;

    set3 = set2 + set1;   
        cout << "set3= set1+ set2 ";
        cout << "set3=";
    cout<<set3;                                 
    cout << endl;

        set4=set1-set2;
        cout << "set4= set1 - set3 ";
        cout << "set4= ";
    cout<<set4;
    cout << endl;
       
        if(set1==set2)
                cout<<"Set1 and Set2 are equal ";
        else
                cout<<"Set1 and Set2 are NOT equal ";
    return 0;
}

Sets are widely used in many computer and real life applications. In this assignment, you will developa class in C++ to represent a set of integers. A set is a collection of distinct integers (i.e. no repetition). It can be represented as an array of Boolean values. For example the set {l, 2, 5} can be represented as a dynamic Boolean array where the array positions 1, 2 and 5 are true. The rest of the array is all false. Sets are widely used in many computer and real life applications. In this assignment, you will develop a n. For example the be value 7 0 6 False True True False False True False False Your class should satisfy the following specifications: A. Name your class "IntSet" with two member variable: set(as dynamic array) and size. B. Provide a constructor to do both initialize an empty set with a maximum of 50 or initialize a set of any maximum. This constructor should also initialize the empty set to false.

Explanation / Answer

Here is the code for IntSet.h:

#include <iostream>
using namespace std;
class IntSet
{
bool *set;
int size;
public:
IntSet()
{
size = 50;
set = new bool(size);
for(int i = 0; i < 50; i++)
set[i] = false;
}
IntSet(int max)
{
size = max;
set = new bool(size);
for(int i = 0; i < max; i++)
set[i] = false;
}
void InsertItem(int x)
{
set[x] = true;
}
~IntSet()
{
delete set;
}
IntSet(IntSet &x)
{
x.size = size;
x.set = new bool(size);
for(int i = 0; i < size; i++)
x.set[i] = set[i];
}
void operator=(IntSet &x)
{
size = x.size;
for(int i = 0; i < size; i++)
set[i] = x.set[i];
}
bool operator==(IntSet x)
{
if(size != x.size)
return false;
for(int i = 0; i < size; i++)
if(set[i] != x.set[i])
return false;
return true;   
}
IntSet &operator-(IntSet x)
{
IntSet y (size);
for(int i = 0; i < size; i++)
y.set[i] = set[i] && x.set[i];
return y;
}
IntSet &operator+(IntSet x)
{
IntSet y (size);
for(int i = 0; i < size; i++)
y.set[i] = set[i] || x.set[i];
return y;
}
friend std::ostream &operator<<(std::ostream &out, IntSet c);
};
ostream &operator<<(std::ostream &out, IntSet x)
{
cout<<"The set is"<<endl;
out<<"{";
for(int i = 0; i < x.size; i++)
if(x.set[i])
out<<i<<", ";
out<<"}"<<endl;
return out;
}

And the code for Main.cpp as you gave is:

#include <iostream>

#include "IntSet.h"

using namespace std;

int main()
{
IntSet set1(10);   
IntSet set2;
IntSet set3;   
IntSet set4;
int i;

  
// reading first 5 odd number in set1
for (i = 1; i < 6; i++)   
{
set1.InsertItem(2*i-1);   
}

cout << endl;
cout << "set1= ";   
cout<<set1;
cout << endl;

set2=set1;
set2.InsertItem(6);
set2.InsertItem(2);
cout << "set2= ";
cout<<set2;
cout << endl;

set3 = set2 + set1;   
cout << "set3= set1+ set2 ";
cout << "set3=";
cout<<set3;   
cout << endl;

set4=set1-set2;
cout << "set4= set1 - set3 ";
cout << "set4= ";
cout<<set4;
cout << endl;

if(set1==set2)
cout<<"Set1 and Set2 are equal ";
else
cout<<"Set1 and Set2 are NOT equal ";
return 0;
}