Write a program to Calculate Difference of set A and B in C++
#include <iostream>
#include <conio.h>
using namespace std;
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<<endl;
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 \n A-B = {";
for(int i=0;i<n;i++)
{ cout<<a[i];
if(i<(n-1))
{ cout<<", "; }}
cout<<"}";
}
int main(int argc, char** argv)
{
cout<<"\t ***** Difference ***** \n";
difference();
getch();
return 0;
}
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.