Write a C program to merge two sorted arrays into another array in a sorted order.

Program Code:
#include<stdio.h>
#include<conio.h>
void main( )
{

int n,m,i,j,k,c[40],a[20],b[20];
printf("Enter how many elements for array A?:\n");
scanf("%d",&n);
printf ("Enter how many elements for array B?:\n");
scanf("%d",&m);
printf("Enter elements for A:-\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter elements for B:-\n");
for(j=0;j<m;j++)
scanf("%d",&b[j]);
i=j=k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
c[k++]=a[i++];
else
if(a[i]>b[j])
c[k++]=b[j++];
else
{
c[k++]=b[j++];
i++;
j++;
}
}
if(i<n)
{
int t;
for(t=0;t<n;t++)
c[k++]=a[i++];
}
if(j<m)
{
int t;
for(t=0;t<m;t++)
{

c[k++]=b[j++];
}
}
printf("\n\n Merged Array C:\n\n")
for(k=0;k<(m+n);k++)
printf("\t \n %d ",c[k]);
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: