Find Union And Intersection Of Two Sets in C++





#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{

    bool U[36],A[36],B[36];
    int i,n,m,e;
    memset(U,true,sizeof U);
    memset(A,false,sizeof A);
    memset(B,false,sizeof B);
    cout<<"Enter Number Of A Set"<<endl;
    cin>>n;
    cout<<"Enter Elements="<<endl;
    for(i=0;i<n;i++)
    {
        cin>>e;
        A[e]=true;
    }
    cout<<"Enter Number Of B Set"<<endl;
    cin>>m;
    cout<<"Enter Elements="<<endl;
    for(i=0;i<m;i++)
    {
        cin>>e;
        B[e]=true;
    }
    cout<<"AUB"<<endl;
    for(i=0;i<36;i++)
    {
        if(A[i] | B[i])
        {
            printf(" %d",i);
        }
    }
     cout<<endl;
    cout<<"A^B"<<endl;
    for(i=0;i<36;i++)
    {
        if(A[i] & B[i])
        {
            printf(" %d",i);
        }

    }
    cout<<endl;
    cout<<"U-A"<<endl;
    for(i=1;i<=36;i++)
    {
        if(U[i] && U[i]^A[i])   //taking uncommon element
        {
            printf(" %d",i);
        }

    }
    cout<<endl;
    cout<<"A-B"<<endl;
    for(i=0;i<36;i++)
    {
        if(A[i] && A[i]^B[i])
        {
            printf(" %d",i);
        }

    }
    cout<<endl;
    return 0;


}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment