Code in C++! Header, cpp, and main file. Most of the code skeleton is already he
ID: 3724612 • Letter: C
Question
Code in C++! Header, cpp, and main file. Most of the code skeleton is already here.
Read instructions:
Create class lntegerSetfor which each object can hold integers in the range 0 through 100. A set is represented internally as an array of ones and zeros. Array element a[] is 1 if integer jis in the set. Array element ali] is 0 if integer j is not in the set. The default constructor initializes a set to the so-called "empty-set," i.e., a set whose array representation contains all zeros Class IntegerSethas the following member functions: 1. inputSet read values from user 2. unionQfSets: create a third set that is the set union of two existing sets (i.e., an element of the third array's is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set's array is set to 0 if that element is 0 in each of the existing sets) 3. create a third set that is the set intersection of two existing sets (i.e., an element of the third array's is set to 1 if that element is1 in both of the existing sets, and an element of the third set's array is set to 0 if that element is 0 in either of the existing sets). 4. printSet: print a set as a list of numbers separated by spaces in between a pair of curly braces. Print only those elements which are present in the set (i.e., their position in the array has a value of 1) The following is a template that you need to use for your program - you need to fill in the necessary components in the template to make it work // Header file for class IntegerSet #ifudef INTEGER-SET-H #define INTEGERSETH - - public: lntegerSetl); // constructor / Write a member funcion prototype for UnionQfSets*/ vaid inputSet0: // read values from user vaid printSet) const; private: int set[ 101]; // range of 0 100Explanation / Answer
#include <iostream>
using namespace std;
class IntegerSet
{
public:
int set[101];
IntegerSet();
void InputSet();
void PrintSet();
};
IntegerSet unionSet(IntegerSet a,IntegerSet b);
IntegerSet IntersectSet(IntegerSet a,IntegerSet b);
int ValidEntry(int x);
IntegerSet ::IntegerSet()
{
for(int i=0;i<=100;i++)
{
set[i]=0;
}
}
void IntegerSet ::InputSet()
{
int number;
do
{
cout<<"enter an element{-1 to end}:"<<endl;
cin>>number;
if(ValidEntry(number))
set[number]=1;
else if(number!=-1)
cout<<"enter valid Element ";
}while(number!=-1);
cout<<"entry Complete ";
}
int ValidEntry(int x)
{
if(x>=0&&x<=100)
return 1;
else
return 0;
}
void IntegerSet ::PrintSet()
{
cout<<"{";
for(int i=0;i<=100;i++)
{
if(set[i])
cout<<i<<" ";
}
cout<<"}";
}
IntegerSet unionSet(IntegerSet a,IntegerSet b)
{
int i=0,j=0;
IntegerSet c;
while(i<=100||j<=100)
{
if(a.set[i]==1)
c.set[i]=1;
i++;
if(b.set[j]==1)
c.set[j]=1;
j++;
}
return c;
}
IntegerSet IntersectSet(IntegerSet a,IntegerSet b)
{
int i=0,j=0;
IntegerSet c;
while(i<=100||j<=100)
{
if((a.set[i]==b.set[j])&&(a.set[i]==1)&&(b.set[j]==1))
c.set[i]=a.set[i];
i++;
j++;
}
return c;
}
int main()
{
IntegerSet a;
IntegerSet b;
IntegerSet d;
IntegerSet e;
cout<<" enter set A: ";
a.InputSet();
cout<<" Set A is: ";
a.PrintSet();
cout<<" enter set B: ";
b.InputSet();
cout<<" Set B is: ";
b.PrintSet();
d=unionSet(a,b);
e=IntersectSet(a,b);
cout<<" Union of set A and B is: ";
d.PrintSet();
cout<<" Intersection of set A and B is: ";
e.PrintSet();
return 0;
// getch();
}