I'm writing a program that works with two sets of numbers put into arrays. The program uses overloaded + and - operators.
Array one = 2 7 4 2 9 7
Array two = 2 9 8 9 1 10 12

The output should be:
For +, New Array = 2 7 4 9 10 8 1 12
For -, New Array = 7 4 2 7
My output is: Addition = 12 10 1 and Subtraction = 12 9 8 9 1 10

Here is the logic for the operators I used to write this code.
The + operator: The object that contains array1 is copied to the object answer.
The array in answer is checked and duplicates are removed. Same thing happens to the second array. Then I bring those two arrays together by inserting whatever is not in array 1 from array 2.

The - operator: The array in object b1 is copied to answer. Based on the size of b2 every element in b2's array is matched to answer's array and then removed. If there is no match, the element is skipped.

I keep getting the wrong output no matter how I alter my code.
What am I doing wrong and how can I fix it?

Any help is Appreciated. Thanks!

   bool set::erase_one(const value_type& target)
    {
         size_type index = 0;  
     while ((index < used) && (data[index] != target))
         {
              ++index;
         }
     if (index == used)
     {
              return false;
         }

         --used; 
     data[index] = data[used];    
     return true;
    }

    bool set::contains(const value_type& target) const
    {
         size_type i;
         for(i = 0; i < used; i++)
         {
               if(target == data[i])
               {
                     return true;
               }
         }
         return false;
    }    

    void set::insert(const value_type& entry)
    // Library facilities used: cassert
    {   
        assert(size( ) < CAPACITY);

        data[used] = entry;
        ++used;
    }

    set operator +(const set& b1, const set& b2)
    {
        size_t index;
        set answer(b1), two(b2);
        for(index = 0; index < answer.size(); index++) 
        {
              int target = answer.data[index]; 
              if(answer.contains(target))
              {
                    answer.erase_one(target);
              }
        }

        for(index = 0; index < two.size(); index++) 
        {
              int target = two.data[index];
              if(two.contains(target))
              {
                    two.erase_one(target);
              }
        }   

        for(index = 0; index < answer.size(); index++) /
        {
              int target = two.data[index];
              if(target != two.data[index])
              {
                    answer.insert(target);
              }
        }
        return answer;
    }

    set operator-(const set& b1, const set& b2)
    {
        size_t index;
        set answer(b1), two(b2); 
        for (index = 0; index < two.size(); ++index)             
        {
            int target = two.data[index]; 
            if (answer.contains(target)) 
            {
               answer.erase_one(target);
            }
    }
    return answer;
    }

From header file:

typedef size_t size_type;
size_type size( ) const { return used; }
int used; //Size of each array
int data[CONSTANT]; //CONSTANT = 30
This article has been dead for over six months. Start a new discussion instead.