"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.

Saturday 10 August 2019

Union, Intersection & Difference Source Code

Write a program to Calculate Union, Intersection & Difference of set A and B in C++ Source Code.



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

// Intersection oF set A and B

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<<"}";
}
void setting(int r[],int r1){

for(int i=0;i<r1;++i)
for(int j=i+1;j<r1;)
{
if(r[i]==r[j])
{
for(int k=j;k<r1-1;++k)
r[k]=r[k+1];
--r1;
}
else
++j;
}
int t;
for (int i=0;i<r1-1;i++)
{
for (int j=i;j<r1;j++)
if(r[i]>r[j])
{
t=r[i];
r[i]=r[j];
r[j]=t;
}
}
cout<<" Union of set A & B\n\n A U B = {";
for(int i=0;i<r1;i++)
{
cout<<r[i];
if(i<(r1-1))
{
cout<<", ";
}
}
cout<<"}";
}

// Union of Set A and B

int Union(){
int n=0,m=0,o=0;
cout<<" Enter size of set A :  ";
cin>>n;
cout<<" Enter size of set B :  ";
cin>>m;
o=n+m;
int a[n], b[m], u[o];
cout<<" Enter elements of set A \n\n";
for(int i=0;i<n;i++)
{
cin>> a[i];
}
cout<<" Enter elements of set B \n\n";
for(int i=0;i<m;i++)
{
cin>> b[i];
}
for(int i=0;i<n;i++)
{
u[i]=a[i];
}
int k=0;
for(int i=n;i<o;i++)
{
u[i]=b[k];
k++;
}
setting(u,o);
}

// Difference of Set A - B

int difference(){
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];
int b[m];
cout<<" Enter elements of set A \n";
for(int i=0;i<n;i++)
{   cin>>  a[i];  }
int t;
for (int i=0;i<n;i++)
{for (int j=i;j<n;j++)
if(a[i]>a[j])
{ t=a[i];
a[i]=a[j];
a[j]=t
;}}
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; }
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
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])
{ t=b[i];
b[i]=b[j];
b[j]=t;
}}
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<m;i++)
cout<<b[i]<<" ";
cout<<"\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if (a[i]==b[j])
{ for( int z=i;z<n;z++)
{
int t=a[z];
a[z]=a[z+1];
a[z+1]=t;

}
n--;}}}
cout<<" Difference of A to B \nA-B = {";
for(int i=0;i<n;i++)
{ cout<<a[i];
if(i<(n-1))
{ cout<<", "; }}
cout<<"}";
}

// Menu of the program

void menu(){
cout<<"\t ======== Welcome ========\n";
cout<<"\t  Enter 1 for Union \n";
cout<<"\t  Entet 2 for Intersection \n";
cout<<"\t  Enter 3 for Difference \n";
cout<<"\t  Enter 0 for Exit \n";
cout<<"\n\n Enter Your Choice : ";
}

// Main Program

int main(int argc, char** argv)
{
int no;
menu();  // call menu
Repeat:
cin>>no;
if ( no > 3 || no < 0 )
{
cout<<" You Are Entered a Wroung Number!! \n Please Try Again ...! \n";
cout<<"\n\n Enter Your Choice : ";
goto Repeat;
}
switch(no)
{
case 0:
{
break;
}
case 1:
{
Union();
break;
}
case 2:
{
intersection();
break;
}
case 3:
{
difference();
break;
}
}
getch();
return 0;
}






Click Here To Download





No comments:

Post a Comment

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