Sunday, 23 October 2016

C++ Program To Find The Union And Intersection Of Two Array In Increasing Order

Problem :-Write A C++ Program To Find The Union And Intersection Of Two Sorted Array In Increasing Order .

Logic :- What is Union ?.

In set theory, the union (denoted by ∪) of a collection of sets is the set of all elements in the collection.[1] It is one of the fundamental operations through which sets can be combined and related to each other. source Wikipedia

What Is Intersection ?.

In mathematics, the intersection A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements. For explanation of the symbols used in this article, refer to the table of mathematical symbols . source Wikipedia

If you have any doubt ask in comment .Till Try Below Yourself  
 
Try This C++ Program For Draw A Perfect Christmas Tree

Solution :-

#include<iostream>
using namespace std;

void unionofarray(int a[],int b[], int m ,int n)
{
 int i=0,j=0;
    cout<<"\n\nUnion Of Array\n\n";
    
while(i<m && j<n)
    {
        if(a[i]<b[j])
    cout<<a[i++]<<" ";
    else if(a[i]>b[j])
    cout<<b[j++]<<" " ;
    else
    {
      cout<<a[i++]<<" ";
      j++;
    }  
    }
  while(i<m)
  cout<<a[i++]<<" ";
  while(j<n)
  cout<<b[j++]<<" ";
}
void intersection(int a[],int b[],int m,int n)
{
  int i=0,j=0;
    cout<<"\n\nIntersection Of Array\n\n";
    while(i<m && j<n)
    {
        if(a[i]<b[j])
    i++;
    else if(a[i]>b[j])
    j++ ;
    else
    {
      cout<<a[i++]<<" ";
      j++;
    }  
    }
}

int main()
{
  int m,i,j,n,a[100],b[100];
 
  cout<<"Enter The Size Of First Array \n";
  cin>>m;
 
  cout<<"\nEnter The Element In First Array \n\n";
 
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"\nEnter The Size Of Second Array \n";
  cin>>n;
 
  cout<<"\nEnter The Element In Second Array \n\n";
 
for(j=0;j<n;j++)
{
cin>>b[j];
}
 
unionofarray(a,b,m,n);
intersection(a,b,m,n);
    
return 0;
}

Output :-


C++ Program To Find The Union And Intersection Of Two Array In Increasing Order

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

subodh yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

2 comments: