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;

}

Greetings,

Through a long debug session, I have discovered why your array seems to zero out. Let's just say, its nothing new ;) Personally, it was the unionOfIntegerSets() and intersectionOfIntegerSets() calls, but don't worry this can be fixed.

It is quite simple. As one may know, returning local variables always perform data loss. If we create a pointer to the data, we can temporarily save the data while passing through the function.

Our first and foremost step is to change the function declaration. Since we are going to return a pointer-type variable, we will need to add the * operator. The unary operator "*" is in indirection of dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.

Inside the class, we must modify our function call to the following:

IntegerSet *unionOfIntegerSets( const IntegerSet& );

The green shows what has been modified. Please note, do the same with intersectionOfIntegerSets()

Secondly, down below we must change our code to:

IntegerSet *IntegerSet::unionOfIntegerSets( const IntegerSet &r ) {   
	IntegerSet temp(size > r.size ? size : r.size);
	IntegerSet *p = &temp;	// Pointer

	p->emptySet();

	int iterations = ( size < r.size ? size : r.size );

	for ( int i = 0; i < iterations; i++ )
		if ( set[ i ] == 1 || r.set[ i ] == 1 )
			p->set[ i ] = 1;

	return p;
}

A lot of stuff to digest, so let's explain this in greater detail.
We know about the return-type. Next we setup temp(), just the way it was done previously; now we create another IntegerSet called p. p is a pointer, and *p is the object of the pointer. The reason p equals "&temp" is because we are sending the address of temp to p.

If this makes sense, we are storing any information temp has inside p. The & operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.

Now any further calls that used to call on temp must use p instead. Remember, if p is a pointer to structure, we would call on the structures members as the following:

p->member-of-structure

If you compile your code as of now, you may retrieve errors. The last thing to fix up is your call in main():

c = *a.unionOfIntegerSets( b );

The reason we added the * operator is because we want the data object of our returned data, not the memory address.

If you have further questions, please feel free to ask.


Hope this helps,
- Stack Overflow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.