C Program to merge two arrays into a single array

#include<stdio.h>
void read(int a[50],int n);
void display(int a[50],int n);
void merge(int a1[50],int a2[50],int n1,int n2);
int main()
{
    int a1[50],a2[50],n1,n2;
    printf("Enter the size of the first array\n");
    scanf("%d",&n1);
    printf("Enter the first array elements\n");
    read(a1,n1);
    printf("The first array elements are:\n");
    display(a1,n1);
    printf("\nEnter the size of the second array\n");
    scanf("%d",&n2);
    printf("Enter the second array elements\n");
    read(a2,n2);
    printf("The second array elements are:\n");
    display(a2,n2);
    merge(a1,a2,n1,n2);

}

void read(int a[50],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void display(int a[50],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }
}

void merge(int a1[50],int a2[50],int n1,int n2)
{
    int i,a3[50],j=0;
    int n3=n1+n2;
    for(i=0;i<n1;i++,j++)
    {
        a3[i]=a1[i];
    }
    for(i=0;i<n3;i++,j++)
    {
        a3[j]=a2[i];
    }
    printf("\nAfter merging the array is:\n");
    display(a3,n3);
}

Output:
C Program to merge two arrays into a single array



Comments