-
C++ need help
Code:
#include<iostream>
using namespace std;
main()
{
int A[5];
int B[5];
cout<<"Enter First 5 elements: "<<endl;
for(int i=0; i<5; i++){
cin>>A[i];
}
cout<<"Enter Second 5 elements: "<<endl;
for(int i=0; i<5; i++){
cin>>B[i];
}
cout<<"Union of Set A and Set B: "<<endl;
cout<<"\nIntersection of Set A and Set B: "<<endl;
for(int i=0; i<5; i++){
for(int t=0; t<5; t++){
if(A[i] == B[t]){
cout<<A[i]<<" ";
}
}
}
cout<<endl;
system("pause");
return 0;
}
hey can someone help me[need help!!!].....on how to solve the union of set a and set b like in ascending order....
Output:
like in
Set A i enter:
1 2 3 4 5
Set B i enter:
1 2 3 5 6
Union of Set A and B
1 2 3 4 5 6
Intersection of Set A and B
1 2 3 4 5
plz need help!..... :) :)
-
Union:
Code:
std::vector<int> setunion;
for(int c1=0;c1<5;c1++)
setunion.push_back(A[c1]);
for(int c2=0;c2<5;c2++)
{
if(!contain_p(B[c2],setunion)
{
setunion.push_back(B[c2]);
}
}
As I'd not give you the whole code..
you need to:
1. Define contain_p() yourself such that it returns a non-zero if setunion contains the given number.
2.Write the code for intersection.
3.Sort the result in ascending order.
This algorithm is rather poor but is sufficient if your input sets are small; also sets are natively supported in c++ to some extent.
-
There are many ways. A simple way is to create a third array, copy the content of both arrays into it, sort it in ascending order, remove duplicates. [And, no, I'm not going to code that - the point of the exercise if for you to learn by doing, not by copying].
Incidentally, your code for printing out the intersection does not sort the values - the values will be out of order if they are input out of order. It will also report duplicates if either array contains duplicates.
You also need to specify that main() returns int (there is no implicit return type in C++).
-
Quote:
Originally Posted by
manasij7479
Union:
Code:
std::vector<int> setunion;
for(int c1=0;c1<5;c1++)
setunion.push_back(A[c1]);
for(int c2=0;c2<5;c2++)
{
if(!contain_p(B[c2],setunion)
{
setunion.push_back(B[c2]);
}
}
As I'd not give you the whole code..
you need to:
1. Define contain_p() yourself such that it returns a non-zero if setunion contains the given number.
2.Write the code for intersection.
3.Sort the result in ascending order.
This algorithm is rather poor but is sufficient if your input sets are small.
how do i print it?
-
If you could work out how to print values in your original code, you can work out how to print out another lot of values.
-
I find it funny that you've posted invalid C++ code that shouldn't compile.
Either you are using a very old compiler (eg Turbo C++), or you are not posting your real code.
So which is it?
Oh, and that indentation is awful. You should spent some time learning how to properly indent™.
-
Quote:
how do i print it?
You can forget that it is a std::vector and print it like a normal array..(querying the size with size()).
Or if you actually want to learn ...read up on STL iterators ...