Pages

Jun 21, 2012

wap in c++ to merge two sorted arrays.


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int a[10],b[10],c[20];
int i,j,k,m,n;
cout<<"\nNo. of elementin A\t";
cin>>n;
cout<<"\nEnter the element in array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nNo. of elementin B\t";
cin>>m;
cout<<"\nEnter the element in array\n";
for(j=0;j<m;j++)
{
cin>>b[j];
}
i=0;j=0;k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
k++;
}


if(i>=n)
{
for(int p=j;p<m;p++)
{
c[k]=b[p];
k++;
}
}
else
{
for(int p=i;p<n;p++)
{
c[k]=a[p];
k++;
}
}
cout<<"\nMerged array\n";
for(i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
getch();
}

No comments:

Post a Comment