Hi again guys,
Im trying to declare SIZE as a constant but i keep getting an error saying C++ forbids declaration of SIZE with no type. Does anybody know how to fix this. it was also giving me an error when i was using the void main command so i switched to int main.
#include<iostream>
using std::cout;
using std::endl;
const SIZE= 101;//0 to 100 set containing 101 elements
class IntegerSet
{
int a[SIZE];
public:
IntegerSet();
IntegerSet(int *, int);
IntegerSet unionOfSets(IntegerSet s2);
IntegerSet intersectionOfSets(IntegerSet s2);
int isEqualTo(IntegerSet s2);
void insertElement(int n);
void printSet();
};
IntegerSet::IntegerSet()//default constructor creates empty set
{
int i;
for(i=0;i<SIZE;i++)
a[i]=0;
}
IntegerSet::IntegerSet(int *ptr, int n)//initialize object using another array of integers
{
int i;
for(i=0;i<SIZE;i++)//Initialize the set first
a[i]=0;
for(i=0;i<n;i++)
a[(ptr[i])]=1;
}
IntegerSet IntegerSet::unionOfSets(IntegerSet s2)
{
IntegerSet s3;
int i;
for(i=0;i<SIZE;i++)
if(a[i]==1 || s2.a[i]==1)
s3.a[i]=1;
return s3;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet s2)
{
IntegerSet s3;
int i;
for(i=0;i<SIZE;i++)
if(a[i]==1 && s2.a[i]==1)
s3.a[i]=1;
return s3;
}
int IntegerSet::isEqualTo(IntegerSet s2)
{
int i;
int check=1;//equal
for(i=0;i<SIZE;i++)
if(a[i]!=s2.a[i])
{
check=0;//not equal
break;
}
return check;
}
void IntegerSet::insertElement(int n)
{
if (n<SIZE && n>=0) //check validity
a[n]=1;
}
void IntegerSet::printSet()
{
int i, count=0;
for(i=0;i<SIZE;i++)
if(a[i]==1)
{
count++;
cout<<i<<" ";
}
if(count==0)
cout<<"---";
}
int main()
{
int arr[4]={3,76,34,56};
IntegerSet s1, s2(arr, 4), s3, s4;
s4.insertElement(34);
s4.insertElement(56);
cout<<"\nDefault set=";
s1.printSet();
cout<<"\nUsing array as parameter, set s2= ";
s2.printSet();
cout<<endl;
cout<<"Inserting 50, 100,1,0,34, 56 to s1, set s1=";
s1.insertElement(50);
s1.insertElement(100);
s1.insertElement(1);
s1.insertElement(0);
s1.insertElement(34);
s1.insertElement(56);
s1.printSet();
cout<<endl;
cout<<"Union of s1 and s2, set s3=";
s3=s1.unionOfSets(s2);
s3.printSet();
cout<<endl;
cout<<"Intersection of s1 and s2, set s3=";
s3=s1.intersectionOfSets(s2);
s3.printSet();
cout<<endl;
cout<<"set s4=";
s4.printSet();
cout<<endl;
cout<<"s2==s3=";
if(s2.isEqualTo(s3)==1)
cout<<"True";
else
cout<<"False";
cout<<"\ns3==s4=";
if(s3.isEqualTo(s4)==1)
cout<<"True";
else
cout<<"False";
system("pause");
return 0;
}