Removing Elements From an Array with Overloaded Operators

I asked this question last night and I didn't get any replies. I figure I'd give it another go.

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
Subtraction: 12 9 8 9 1 10


I'm getting the wrong output and I don't see what I am doing wrong.
How can I fix it?

Any help is Appreciated. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
   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 my header:
1
2
3
4
typedef size_t size_type;
size_type size( ) const { return used; }
int used; //Size of each array
int data[CONSTANT]; //CONSTANT = 30 
insert() should deal with duplicates and ordering.

Why does operator+ call erase_one? And why does it make a copy of b2? You should be able to just insert values and let the set deal with duplicates.

Topic archived. No new replies allowed.