EmbLogic's Blog

C program to merge two aaray

#include
int main()
{
int a[100],b[100],i,j,k,l,c[100],m,n,o,x,y;
printf(“How many no. do you want to add in array a[100]:”);
scanf(“%d”,&n);
printf(“Enter element of first array:\n”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("How many no. do you want to add in array b[100]:");
scanf("%d",&o);
printf("Enter element of second array:\n");
for(i=0;i<o;i++)
{
scanf("%d",&b[i]);
}
for(i=0,k=0;i<n,k<o;i++,k++)
{
for(j=i+1,l=k+1;j<n,la[j])
{
x=a[i];
a[i]=a[j];
a[j]=x;
}
if(b[k]>b[l])
{
y=b[k];
b[k]=b[l];
b[l]=y;
}
}
}
printf(“Element of array a[100] after sorting:\n”);
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
printf("Element of array b[100] after sorting:\n");
for(i=0;i<o;i++)
{
printf("b[%d]=%d\n",i,b[i]);
}
j=0;k=0;
for(i=0;i<n+o;i++)
{
if(j<n && k<o)
{
if(a[j]<b[k])
{
c[i]=a[j];
j++;
}
else
{
c[i]=b[k];
k++;
}
}
else if(j==n)
{
for(;i<n+o;i++)
{
c[i]=b[k];
k++;
}
}
else
{
for(;i<n+o;i++)
{
c[i]=a[j];
j++;
}
}
}
printf("merged array is:\n");
for(i=0;i<n+o;i++)
{
printf("c[%d]=%d\n",i,c[i]);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>