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.
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 = newint[dl_a];
int * drugi = newint[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
void symmetric_diff(Set &a)
{
int n = 0;
int m = 0;
int * tab = newint[number];
int * tab2 = newint[a.number];
int * mer = newint[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");
}