C Program to Merge Two Arrays
Questions: Write a program in C to read two arrays having m and n elements then merge these two arrays into one single array.
C Program to Merge Two Array
#include<stdio.h>
int main()
{
int a[100], b[100],r[200],i, m, n, pos;
clrscr();
/* Reading first array */
printf("Enter m:\n");
scanf("%d", &m);
printf("Enter elements of first array:\n");
for(i=0;i< m;i++)
{
printf("a[%d]=",i);
scanf("%d", &a[i]);
}
/* Reading second array */
printf("Enter n:\n");
scanf("%d",&n);
printf("Enter elements of second array:\n");
for(i=0;i< n;i++)
{
printf("b[%d]=",i);
scanf("%d", &b[i]);
}
/* Merging */
for(i=0;i< m;i++)
{
r[i] = a[i];
}
for(i=0;i< m+n;i++)
{
r[m+i] = b[i];
}
/* Displaying merged array */
printf("Merged array is:\n");
for(i=0;i< m+n;i++)
{
printf("%d\t",r[i]);
}
return 0;
}