"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Monday, 5 August 2019

Intersection Of Sets A and B Source program

Write a program to calculate intersection of set A and B in C++.



#include <iostream>
#include <conio.h>
using namespace std;

// Intersection portion

int intersection(){
int v;
int n=0,m=0,o=0;
cout<<" Enter size of Set A :  ";
cin>>n;
cout<<" Enter size of Set B :  ";
cin>>m;
int a[n],b[m],c[o];
cout<<" Enter Elements of Set A \n";
for(int i=0;i<n;i++)
{
   cin>> a[i];  
}
for (int i=0;i<n;i++){
for (int j=i;j<n;j++)
if(a[i]>a[j])
{ v=a[i];
a[i]=a[j];
a[j]=v;
}
}
for(int i=0;i<n;++i)
for(int j=i+1;j<n;)
{
if(a[i]==a[j])
{ for(int k=j;k<n-1;++k)
a[k]=a[k+1];
--n; }
else
++j; }
cout<<" Enter Elements of Set B \n";
for(int i=0;i<m;i++)
{
cin>> b[i]; }
for (int i=0;i<m;i++)
{
for (int j=i;j<m;j++)
if(b[i]>b[j])
{
v=b[i];
b[i]=b[j];
b[j]=v;
}
}
for(int i=0;i<m;++i)
for(int j=i+1;j<m;)
{
if(b[i]==b[j])
{
for(int k=j;k<m-1;++k)
b[k]=b[k+1];
--m; }
else
++j;
}
for( int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i]==b[j])
{
c[o]=a[i];
o++;
}
}
}
cout<<" A Intersection B = {";
for(int i=0;i<o;i++)
{
cout<<c[i];
if(i<(o-1))
{
cout<<", ";
}
cout<<"}";
}

int main(){
cout<<"\t ***** INTERSECTION *****\n";
intersection();
getch();
return 0;
}


No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.