#include <iostream>
#include <conio.h>
usingnamespace std;
// Classes
int Setarray(); // Inputs the numbers into Sets
int main()
{
cout << Setarray();
}
int Setarray(){
constint 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}.
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.