Array Applications
Array Applications
#include<iostream>
using namespace std;
arr3[k] = arr1[i];
k++;
i++;
}
else{
arr3[k] = arr2[j];
k++;
j++;
}
arr3[k] = arr2[j];
k++;
j++;
}
}
int main()
{
int arr1[] = {2, 3, 5, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr3[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
return 0;
}
Program to find common elements in an array:
#include <iostream>
# define Z 50
void findCommonElement( int A[], int n, int B[], int m, int D[], int &k ) {
k = 0;
for( int i = 0; i < n; i++ ) {
for( int j = 0; j < m; j++ ) {
if( A[ i ] == B[ j ] ) {
D[ k ] = A[ i ];
k = k + 1;
}
}
}
}
int main() {
int n,m,A[Z],B[Z];
cout<<"Enter size of the first array: "<<endl;
cin>>n;
cout<<"Enter "<<n<<" elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>A[i];
}
cout<<"Enter size of the second array: "<<endl;
cin>>m;
cout<<"Enter "<<m<<" elements"<<endl;
for(int i=0;i<m;i++)
{
cin>>B[i];
}
int D[ Z ];
int k = 0;
findCommonElement( A, n, B, m, D, k );
cout << "The common elements are: ";
displayArr( D, k );
}