Program to print the common values of two 1D Arrays. (Intersection Values)
#include<iostream.h>
void main()
{
int a[]={1,2,4,5,6};
int b[]={2,3,5,7};
int n1, n2, i=0, j=0;
n1=sizeof(a)/sizeof(int);
n2=sizeof(b)/sizeof(int);
cout<<"The Intersection Program:\n";
cout<<"=========================\n";
cout<<"The value of 1st Array are: ";
for(i=0;i<n1;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"The value of 2nd Array are: ";
for(i=0;i<n2;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<"The common values in two arrays are: ";
for(i=0,j=0;i<n1 && j<n2;)
{
if(a[i]<b[j])
i++;
else if(b[j]<a[i])
j++;
else
{
cout<<b[j++]<<" ";
i++;
}
}
getch();
}
void main()
{
int a[]={1,2,4,5,6};
int b[]={2,3,5,7};
int n1, n2, i=0, j=0;
n1=sizeof(a)/sizeof(int);
n2=sizeof(b)/sizeof(int);
cout<<"The Intersection Program:\n";
cout<<"=========================\n";
cout<<"The value of 1st Array are: ";
for(i=0;i<n1;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"The value of 2nd Array are: ";
for(i=0;i<n2;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<"The common values in two arrays are: ";
for(i=0,j=0;i<n1 && j<n2;)
{
if(a[i]<b[j])
i++;
else if(b[j]<a[i])
j++;
else
{
cout<<b[j++]<<" ";
i++;
}
}
getch();
}
No comments
Post your comments