Need and Algorythm for Finding Union Of Sets.

Hey I am new To C++ programming , and I am just making a new Program to Find the Union Of Two Sets. Here is the Code

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
  #include <iostream>
#include <conio.h>

using namespace std;

// Classes
int Setarray(); // Inputs the numbers into Sets

int main()
{

cout << Setarray();

}


int Setarray(){
const int size = 100;
int setA[size];
int userdefined;
int i;
cout << "Enter the Number of Elements in Set A \n"<< endl;
cin>> userdefined;

if(userdefined>size){
        cout << " \nWhoa Hold it right there, \n I can't Hold that much Numbers You making Fun of Me ? , Enter a Number Less than 100\n";
        cin >> userdefined;

}

cout << " \nEnter the numbers one by one , Press Enter after you have Entered the number \n ( Make sure you Don't Enter the same number Twice !!) \n";

for(int i=0; i<userdefined; i++ ){

cin>> setA[i];

if(setA[i]==setA[i-1]){

    cout << "Seems Like You Have entered The Same Number Twice , Please Enter It again. \n";
    cin>> setA[i];
}

}

cout << "\n {";

for(int i=0; i<userdefined; i++){

cout << setA[i]<<",";

}

cout << "} \n \n";



int setB[size];
int x;
cout << "Enter the Number of Elements in Set B \n"<< endl;
cin>> userdefined;

if(userdefined>size){
        cout << " \nWhoa Hold it right there, \n I can't Hold that much Numbers You making Fun of Me ? , Enter a Number Less than 100\n";
        cin >> userdefined;

}

cout << " \nEnter the numbers one by one , Press Enter after you have Entered the number \n ( Make sure you Don't Enter the same number Twice !!) \n";

for(int x=0; x<userdefined; x++ ){

cin>> setB[x];

if(setB[x]==setB[x-1]){

    cout << "Seems Like You Have entered The Same Number Twice , Please Enter It again. \n";
    cin>> setB[x];
}


}

cout << "{";

for(int x=0; x<userdefined; x++){

cout << setB[x]<<",";

}

cout << "} \n";

}


I know the code is Pretty Wierd ( Hey Iam just 14)

What i want To Do is Combine The Elements in Array : setA , and Array: setB , So that the Resulting Array Should Contain The Union Of These elements.

By Union I mean That if SetA contains {1,2,3,4,} and SetB contains {1,6,2,4,8}
then the resulting Set C must conatain {1,2,3,4,6,8}.

Any help is welcome ;)


Kudos for trying to checking if the user entered the same number twice.
However, there are a couple of problems with that code.

1) It only works if the user entered the same number consecutively. It won't detect if the user entered the same number non-consecutively. i.e. 1,3,1...

2) You're making an out of bounds reference by refering to setA[i-1]. The first time though the loop, i will be 0 and you'll be trying to compare setA[-1], which is not a valid reference.

Some other comments:
Since the process of prompt the user for the sets is the same, you might consider putting that code in a function and calling the function to get the contents of each set, rather than repeating the code.

Your cout at line 87 is going to output an extraneous , after the last element. Not a biggie.

If you want to learn how two merge the two sets rather than using the set_union as Yanson suggested, what you are going to need to do is:
1) Sort each of the two sets. You can write your own sort, or there are sort algorithms provided in the standard library. After each set is sorted is a good time to check for duplicates.
2) Iterate through the two sets. If a[i] < b[j], then move a[i] to the output set and advance i. if a[i] == b[j], move a[i] to the output set and advance both i and j. if a[i] > b[j], move b[j] to the output set and advance j. Quit when i = limit of a and j = limit of b. Note i and j may not reach their limits at the same time, so you may have the condition when i = limit of a and elements still exist to be moved in b and visa versa.

Last edited on
Topic archived. No new replies allowed.