hello there, when I try to combine 2 arrays together it displays "0".
I think I don't understand the concept of constant. This is my code:
#ifndef INTSET_H
#define INTSET_H
class IntegerSet {
public:
IntegerSet( int );
IntegerSet( const IntegerSet& );
IntegerSet unionOfIntegerSets( const IntegerSet& );
IntegerSet intersectionOfIntegerSets( const IntegerSet& );
void emptySet();
void inputSet();
void insertElement( int );
void deleteElement( int );
void setPrint() const;
bool isEqualTo( const IntegerSet& ) const;
private:
int *set;
int size;
bool validEntry( int x ) const
{
return x >= 0 && x < size;
}
};
#endif
#include <iostream>
#include <iomanip>
#include <new>
using std::cout;
using std::cin;
using std::setw;
#include "integerset.h"
IntegerSet::IntegerSet( int s )
{
size = s;
set = new int[ size ];
emptySet();
}
IntegerSet::IntegerSet( const IntegerSet &init )
{
size = init.size;
set = new int[ size ]; /* write statement to allocate sufficient memory */
emptySet();
for ( int i = 0; i < size; i++ )
insertElement(set[i]); /* write statement to copy elements of init */
}
void IntegerSet::emptySet()
{
for ( int i = 0; i < size; i++ )
set[ i ] = 0;
}
void IntegerSet::inputSet()
{
int number;
// input set information
do {
cout << "Enter an element (-1 to end): ";
cin >> number;
// check number first
if ( validEntry( number ) )
set[ number ] = 1;
else if ( number != -1 )
cout << "Invalid Element\n";
} while ( number != -1 );
cout << "Entry complete\n";
}
void IntegerSet::setPrint() const
{
int x = 1;
bool empty = true; // assume set is empty
cout << '{';
for ( int u = 0; u < size; ++u )
if ( set[ u ] ) {
cout << setw( 4 ) << u << ( x % 10 == 0 ? "\n" : "" );
empty = false; // set is not empty
++x;
}
if ( empty )
cout << setw( 4 ) << "---"; // display an empty set
cout << setw( 4 ) << "}" << '\n';
}
IntegerSet IntegerSet::unionOfIntegerSets( const IntegerSet &r )
{
IntegerSet temp( size > r.size ? size : r.size );
temp.emptySet();
int iterations = ( size < r.size ? size : r.size );
for ( int i = 0; i < iterations; i++ )
if ( set[ i ] == 1 || r.set[ i ] == 1 )
temp.set[ i ] = 1;
return temp;
}
IntegerSet IntegerSet::intersectionOfIntegerSets( const IntegerSet &r )
{
IntegerSet inter( size > r.size ? size : r.size );
inter.emptySet();
int reduction = ( size < r.size ? size : r.size );
for ( int i = 0; i < reduction; i++ )
if ( set[ i ] == 1 && r.set[ i ] == 1 )
inter.set[ i ] = 1;
return inter;
}
void IntegerSet::insertElement( int k )
{
if ( validEntry( k ) )
set[ k ] = 1;
else
cout << "Invalid insert attempted!\n";
}
void IntegerSet::deleteElement( int d )
{
if ( validEntry( d ) )
set[ d ] = 0;
else
cout << "Invalid delete attempted!\n";
}
bool IntegerSet::isEqualTo( const IntegerSet& eql ) const
{
for ( int i = 0; i < size; i++ )
if ( set[i] = eql.set[i] )
return true;
else
return false;
}
#include <iostream>
using std::cout;
using std::endl;
#include "integerset.h"
int main()
{
IntegerSet a( 101 );
IntegerSet b( 101 );
IntegerSet c( 101 );
IntegerSet d( 101 );
cout << "Enter set A:\n";
a.inputSet();
cout << "\nEnter set B:\n";
b.inputSet();
c = a.unionOfIntegerSets( b );
d = a.intersectionOfIntegerSets( b );
cout << "\nUnion of A and B is:\n";
c.setPrint();
cout << "Intersection of A and B is:\n";
d.setPrint();
if ( a.isEqualTo( b ) )
cout << "Set A is equal to set B\n";
else
cout << "Set A is not equal to set B\n";
cout << "\nInserting 77 into set A...\n";
a.insertElement( 77 );
cout << "Set A is now:\n";
a.setPrint();
cout << "\nDeleting 77 from set A...\n";
a.deleteElement( 77 );
cout << "Set A is now:\n";
a.setPrint();
b.setPrint();
cout << endl;
return 0;
}