[C++] - sets and operations on them

Hello,

I have struggled with C++ for a few years now, and would like to finally have it covered on my classes. The problem is my skills are still very weak. No matter how hard I have tried, I could not improve very much.

Right now I have a project that will allow me to pass. The poject is about sets and operations on them. Here is what needs to be done:

1) Create a class that is storing sets and allows the following operations:
a) input the sets from the keyboard
b) print the sets
c) show the sum of the sets (A u B)
d) show the intersection (A n B)
e) show the symetrical difference (A - B) = (A u B) \ (A n B)
f) show the relative complement (A \ B) and (B \ A)
g) add and remove elements from the sets

2) Build a program that will test the class.

I'm afraid that the only thing that is working is printing the sets. Oddly, when I try to print the sum of the sets (as well as some other), I get "cosmic" numbers.
The program compiles fine in Dev C++ so this may make it easier to find the problem.

Some of the variables are named using polish language, hopefully this will not affect anything. The most important ones are in "english" though.

I would really appreciate any help, as I'm a little bit in dire straits here.

Thank you,
Wojciech.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>

using namespace std;

class Set{
public:
	int	number;
	int	* elems;
    
	Set(int tab[], int n)
	{
	    number = n;
		elems = new int[n];
		for(int i = 0; i < n; i++) elems[i] = tab[i];
    }
	
	Set(Set &obj)
	{
        number = obj.number;
		elems = new int[number];
		for(int i = 0; i < number; i++) elems[i] = obj.elems[i];
    }
    
	~Set()
    {
		delete [] elems;
    }
    
    void swap(int &a, int &b)
    {
         int temp = a;
         a = b;
         b = temp;
    }
    
    void bubblesort(int tab[], int n)
    {
         int p;
         for (int i=n-1; i>0; i--) 
         { 
         p=1; 
         for (int j=0; j<i; j++) 
           if (tab[j] > tab[j+1]) 
           { 
               swap(tab[j], tab[j+1]); 
               p=0; 
           } 
       if (p) break; 
       } 
    } 
    
    void rdup(int * tab, int n)
    {
         int *current;
         int *end = tab + n - 1;

          for ( current = tab + 1; tab < end; tab++, current = tab + 1 )
          {
              while ( current < end )
               {
            if ( *current == *tab )
            {
                *current = *end--;
            }
            else
            {
                current++;
            }
        }
    }
}    
         
	
	void sum(Set &a)
	{
		int	n = number;
		int * tab = new int[n + a.number];
		bool czy;
		
		for(int i = 0; i < n; i++) tab[i] = elems[i];
		
		for(int i = 0; i < a.number; i++){
  	    czy = false;
			for(int j = 0; j < n; j++){
				if(elems[j] == a.elems[i]) czy = true;
				else czy = czy | false;}
			
			if(!czy){
				tab[n] = a.elems[i];
				n++;}}
		
        bubblesort(tab, a.number);
        rdup(tab, a.number);		
		system("cls");
        cout << "Sum of the sets is: ";		
		for (int i = 0; i < n; i++)
        cout << tab[n] << " "; 
        
        delete [] tab;
        cout << endl;
        system("PAUSE");
    }
    
    void relative_complement(Set &a)
    {
         int n = 0;
         int * tab = new int[number];
         bool czy;
         
         for (int i = 0; i < number; i++){
             czy = false;
             for (int j = 0; j < a.number; j++){
                 if(elems[i] == a.elems[j]) czy = true;
                 else czy = czy | false;}
    
             if(!czy){
                 tab[n] = elems[i];
                 n++;}}
             
        bubblesort(tab, number);
        rdup(tab, number);         
        system("cls");
        cout << "The relative complement of the sets is: ";		
        for (int i = 0; i < n; i++)
        cout << tab[n] << " "; 
        cout << endl;        
        delete [] tab;
        system("PAUSE");      
                 
    }
    
    void symmetric_diff(Set &a)
    {
         int n = 0;
         int m = 0;
         int * tab = new int[number];
         int * tab2 = new int[a.number];
         int * mer = new int[number + a.number]; 
         bool czy;
         
         for (int i = 0; i < number; i++){
             czy = false;
             for (int j = 0; j < number; j++){
                 if (elems[i] == a.elems[j]) czy = true;
                 else czy = czy | false;}
         
         if(!czy){
                 tab[n] = elems[i];
                 n++;}}
                 
         for (int k = 0; k < a.number; k++){
             czy = false;
             for (int l = 0; l < number; l++){
                 if (a.elems[k] == elems[l]) czy = true;
                 else czy = czy | false;}
                 
         if (!czy){
                   tab2[m] = a.elems[k];
                   m++;}}
         
         for (int o = 0; o < number; o++)
             mer[o] = tab[o];
         
         for (int p = number; p < number + a.number; p++)
             mer[p] = tab[p-number];
                  
         bubblesort(mer, number + a.number);
         rdup(mer, number + a.number);
         cout << "The symetric difference of the sets is: ";         
         for (int q = 0; q < number + a.number; q++)
         cout << mer[q] << " ";    
         
         system("PAUSE");
    }        
  
	void intersection (Set &a)
	{
		int	n = 0;
	    int * tab = new int[(number > a.number) ? number : a.number];
		bool czy;
		
		for(int i = 0; i < a.number; i++){
			czy = false;
			for(int j = 0; j < number; j++){
				if(elems[j] == a.elems[i]) czy = true;
				else czy = czy | false;}
			
			if(czy){
				tab[n] = a.elems[i];
				n++;}}
		
		bubblesort(tab, a.number);
		rdup(tab, a.number);
        system("cls");
        cout << "The intersection of the sets is: ";		
        for (int i = 0; i < n; i++)
        cout << tab[n] << " "; 

        delete [] tab;
        cout << endl;        
        system("PAUSE");
    }
    
    void dodaj(int n)
    {
         int * temp = new int[number];
         for (int i = 0; i < number; i++)
         temp[i] = elems[i];
         delete [] elems;
         elems = new int[number + 1];
         for (int i = 0; i < number; i++)
         elems[i] = temp[i];
         delete [] temp;
         elems[number] = n;
    }
    
    void usun(int n)
    {
         int usun = n;
         int * temp = new int[number];
         for (int i = 0; i < number; i++)
         temp[i] = elems[i];
         delete [] elems;
         number = number - 1;
         elems = new int[number];
         for (int i = 0; i < usun; i++)
         elems[i] = temp[i];
         for (int i = usun; i = number; i++)
         elems[i-1] = temp[i];
         delete [] temp; 
    }
	
	Set create(Set &a)
	{
		number = a.number;
		delete elems;
		elems = new int[number];
		for(int i = 0; i < number; i++) elems[i] = a.elems[i];
    }
    
    void show()
	{
		for(int i = 0; i < number; i++) cout << elems[i] << " ";
		cout << endl;
		system("PAUSE");
    }
};
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

int main()
{
    int wybor;
    int ktory_zbior;
    int ktory_powieksz, jaki_powieksz;
    int ktory_usun, jaki_usun;
    int ktora_roznica;
    
	cout << "Enter the size of the first set" << endl;
	int dl_a;
	cin >> dl_a;
	
	cout << "\nEnter the size of the second set" << endl;
	int dl_b;
	cin >>dl_b;
	
	int * pierwszy = new int[dl_a];
	int * drugi = new int[dl_b];
	
	cin.get();
	system("cls");
	
	cout << "Populating the first set" << endl;
	for (int i = 0; i < dl_a; i++){
        cout << "\nEnter the " << i+1 << " element of the set ";
        cin >> pierwszy[i];
        }
 
    cin.get();
    system("cls");
    
    cout << "Populating the second set" << endl;
    for (int j = 0; j < dl_b; j++){
        cout << "\nEnter the " << j+1 << " element of the set ";
        cin >> drugi[j];
        }
    
    cin.get();
    system("cls");
    
    Set a(pierwszy, dl_a);
    Set b(drugi, dl_b);
    
    do {
    system("cls");
    cout << "We have two sets now. What do you want to do now?" << endl;
	cout << "1. Print the set?" << endl;
	cout << "2. Print the sum of the sets?" << endl;
	cout << "3. Print the intersection of the sets?" << endl;
	cout << "4. Print the relative complement/symetric difference of the sets?" << endl;
	cout << "5. Add an element to the set?" << endl;
	cout << "6. Remove an element from the set?" << endl;
    cout << "7. Quit?" << endl;
	cin >> wybor;
    
    switch (wybor) { 
           case 1:
                {
                     cout << endl;
                     cout << "Which set?" << endl;
                     cin >> ktory_zbior;
                     cout << endl;
                     if (ktory_zbior == 1)
                        a.show();
                        else
                        b.show();
                     break;
                }
           case 2:
                {
                        a.sum(b);
                        break;
                }     
           case 3:
                {
                      a.intersection(b);
                      break;
                }
                
           case 4:
                {
                      cout << endl;
                      cout << "1. A-B" << endl;
                      cout << "2. B-A" << endl;
                      cout << "3. symetrical difference" << endl;
                      cin >> ktora_roznica;
                      
                      switch (ktora_roznica){
                             case 1:
                                  {
                                             a.relative_complement(b);
                                             break;
                                  }
                             case 2:
                                  {
                                             b.relative_complement(a);
                                             break;
                                  }
                             case 3:         
                                  {
                                             a.symmetric_diff(b);
                                             break;           
                                  }
                      }
                      break;
                }
           case 5:
                {
                      cout << "Which set should be expaned?";
                      cin >> ktory_powieksz;
                      cout << "\nWhat element should be added?";
                      cin >> jaki_powieksz;
                      
                      if (ktory_powieksz == 1)
                      a.dodaj(jaki_powieksz);
                      else
                      b.dodaj(jaki_powieksz);
                      break;
                }                   
            case 6:
                 {
                      cout << "Which set should be shrinked?";
                      cin >> ktory_usun;
                      cout << "\nCounting from the left, which element you want to remove?";
                      cin >> jaki_usun;
                      if (ktory_usun == 1)
                      a.usun(jaki_usun);
                      else
                      b.usun(jaki_usun);
                      break;
                 }              
           }    
    }
	while (wybor != 7);
	
    system("PAUSE");
}
Mind posting the problem code, or at least telling us the problem lines? I don't know about other people on here, but I sure don't want to look through almost 400 lines of code looking for some random error
Hello,

Sorry about that. At the moment there are three issues that are critical for me to solve.

1) sorting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void bubblesort(int tab[], int n)
    {
         int p;
         for (int i=n-1; i>0; i--) 
         { 
         p=1; 
         for (int j=0; j<i; j++) 
           if (tab[j] > tab[j+1]) 
           { 
               swap(tab[j], tab[j+1]); 
               p=0; 
           } 
       if (p) break; 
       } 
    } 



2) symetrical difference

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
void symmetric_diff(Set &a)
    {
         int n = 0;
         int m = 0;
         int * tab = new int[number];
         int * tab2 = new int[a.number];
         int * mer = new int[number + a.number]; 
         bool czy;
         
         for (int i = 0; i < number; i++){
             czy = false;
             for (int j = 0; j < number; j++){
                 if (elems[i] == a.elems[j]) czy = true;
                 else czy = czy | false;}
         
         if(!czy){
                 tab[n] = elems[i];
                 n++;}}
                 
         for (int k = 0; k < a.number; k++){
             czy = false;
             for (int l = 0; l < number; l++){
                 if (a.elems[k] == elems[l]) czy = true;
                 else czy = czy | false;}
                 
         if (!czy){
                   tab2[m] = a.elems[k];
                   m++;}}
         
         for (int o = 0; o < number; o++)
             mer[o] = tab[o];
         
         for (int p = number; p < number + a.number; p++)
             mer[p] = tab[p-number];
                  
         bubblesort(mer, number + a.number);
         rdup(mer, number + a.number);
         cout << "The symetric difference of the sets is: ";         
         for (int q = 0; q < number + a.number; q++)
         cout << mer[q] << " ";    
         
         system("PAUSE");
    }        


Thank you,
Last edited on
Topic archived. No new replies allowed.