All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Thursday, 27 March 2014

Merging two Accending Arrays

//Merging two Accending Arrays
#include<iostream.h>
int main()
{
int a[50],b[50];
int i,j,k,m,n;
cout<<"enter no of elements in A ";
cin>>m;
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"enter no of elements in B ";
cin>>n;
for(j=0;j<n;j++)
{
cin>>b[j];
}
//////////////////////////////////////
int s=m+n;
int p=0;j=0;
label:i=p;
for(i=p;i<m;i++)
{
if(b[j]<a[i])
{
for(k=m;k>i;k--)
{
a[k]=a[k-1];
}
a[i]=b[j];
m++;
j++;
p=i+1;
goto label;
}
}

if(m<s)
{
 for(i=j;i<=n;i++,j++)
 {
  a[m]=b[j];
  m++;
 }
}
for(i=0;i<s;i++)
{
cout<<a[i]<<" ";
}
return 0;
}

No comments:

Post a Comment