#include<iostream>
#include<conio.h>
using namespace std;
int merge(int a[],int m,int b[],int n,int c[],int p);
int main()
{
int a[10],b[10],c[10];
int m,n,p;
cout<<"\n Enter size of First array";
cin>>m;
cout<<"\n Enter element of First array";
for(int i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"\n Enter size of Second array";
cin>>n;
cout<<"\n Enter element of Second array";
for(int i=0;i<n;i++)
{
cin>>b[i];
}
merge(a,m,b,n,c,p);
cout<<"\n after merging : ";
for(int k=0;k<m+n;k++)
{
cout<<" "<<c[k];
}
getch();
}
int merge(int a[10],int m,int b[10],int n,int c[10],int p)
{
int i=0;
int j=0;
int k=0;
while(i<m && j<n)
{
if(a[i]<=b[j])
{
c[k]=a[i];
i++;
}
else if(a[i]>=b[j])
{
c[k]=b[j];
j++;
}
k++;
}
if(i==m && j<n)
{
c[k]=b[j];
j++;
k++;
}
if(j==n && i<m)
{
c[k]=a[i];
i++;
k++;
}
}
output:
No comments:
Post a Comment