Given two sorted arrays: A
and B
. The size of array A
is La
and the size of array B
is Lb
. How
void Intersect()
{
int la, lb;
la = 5;
lb = 100;
int A[5];
int i, j, k;
i = j = k = 0;
for (; i < 5; ++i)
A[i] = i + 1;
int B[100];
for (; j < 100; ++j)
B[j] = j + 2;
int newSize = la < lb ? la : lb;
int* C = new int[newSize];
i = j = 0;
for (; k < lb && i < la && j < lb; ++k)
{
if (A[i] < B[j])
i++;
else if (A[i] > B[j])
j++;
else
{
C[k] = A[i];
i++;
j++;
}
}
for (k = 0; k < newSize; ++k)
cout << C[k] << NEWLINE;
}
//intersection of two arrays
#include<iostream>
using namespace std;
int main() {
int i=0,j=0,m,n;
int arr1[i],arr2[j];
cout<<"Enter the number of elements in array 1: ";
cin>> m;
cout<<"Enter the number of elements in array 2: ";
cin>>n;
for (i=0;i<m;i++){
cin>> arr1[i];
}
for(j=0;j<n;j++){
cin>> arr2[j];
}
for(j=0;j<n;j++){
for(i=0;i<m;i++) {
if (arr1[i] == arr2[j]){
cout<< arr1[i];
cout << ' ';
break;
}
}
}
return 0;
}