C Program to Merge Two Arrays

In this tutorial, we will learn about how to merge any two arrays to form the third array that contains all the elements of two given array. We will also learn about merging arrays in ascending and descending order.

Merge Two Arrays in C

To merge any two array in C programming, start adding each and every element of the first array to the third array (target array). Then start appending each and every element of the second array to the third array (target array) as shown in the program given below.

Following C program ask from the user to enter array 1 size and its element and then to enter array 2 size and its element, to merge and form the new array (target array or third array), then display the result of the merged array. Here we have displayed the merged array directly. You can also sort the two arrays before merge. So to learn about sorting, here are the list of three techniques to sort any array:

Now let's take a look at the program of merging two given array. The question is, write a program in C to merge any two arrays entered by user at run-time. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr1[50], arr2[50], size1, size2, i, k, merge[100];
    printf("Enter Array 1 Size: ");
    scanf("%d", &size1);
    printf("Enter Array 1 Elements: ");
    for(i=0; i<size1; i++)
    {
        scanf("%d", &arr1[i]);
        merge[i] = arr1[i];
    }
    k = i;
    printf("\nEnter Array 2 Size: ");
    scanf("%d", &size2);
    printf("Enter Array 2 Elements: ");
    for(i=0; i<size2; i++)
    {
        scanf("%d", &arr2[i]);
        merge[k] = arr2[i];
        k++;
    }
    printf("\nThe new array after merging is:\n");
    for(i=0; i<k; i++)
        printf("%d ", merge[i]);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run you will get the following output on your output screen:

c program merge two array

Now supply the size and elements of first array and then size and elements of second array. Press ENTER key to see the merged array as output:

merge two array c language

Program Explained

  • Receive size for first array say 4
  • Then receive 4 elements for the first array
  • While receiving the first array elements inside the for loop, initialize each and every element to the merge array after receiving
  • That is the first, second, third and fourth elements of the first arrays are initialized to the merging array at index number, merge[0], merge[1], merge[2], and merge[3]
  • After receiving all the 4 elements for first array
  • Initialize the value of variable i to any variable say k. Now k holds the value 4 (in this case)
  • Receive size for second array say 4
  • Then receive 4 elements for the second array
  • While receiving the second array elements inside the for loop, initialize each and every elements to the merge array after receiving
  • This time, start merge array's index from where the last indexing ends, that is 4 (the starting value of k)
  • That is the first, second, third and fourth elements of the second arrays are initialized to the merging array at index number, merge[4], merge[5], merge[6], and merge[7]
  • Now create a loop that starts from 0 to the value that the variable k holds, here in this case 0 to 7
  • And print the merged array inside the loop

Merge Two Arrays in Ascending Order

The question is, write a program in C to read values in two Array A and B, merge elements of both the array into third Array C in ascending order. Print all the array at last. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[10], b[10], c[20], i, j, limitC, temp;
    printf("Enter 10 elements in array A:");
    for(i=0; i<10; i++)
        scanf("%d", &a[i]);
    printf("Enter 10 elements in array B:");
    for(i=0; i<10; i++)
        scanf("%d", &b[i]);
    printf("\nElements of Array A are:\n");
    for(i=0; i<10; i++)
    {
        if(i==9)
            printf("%d", a[i]);
        else
            printf("%d, ", a[i]);
    }
    printf("\n\nElements of Array B are:\n");
    for(i=0; i<10; i++)
    {
        if(i==9)
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
	
    // merging the two arrays
    for(i=0; i<10; i++)
        c[i] = a[i];
    for(j=0; j<10; j++)
    {
        c[i] = b[j];
        i++;
    }
	
    // sorting the merged array
    for(j=0; j<19; j++)
    {
        for(i=0; i<19; i++)
        {
            if(c[i]>c[i+1])
            {
                temp = c[i];
                c[i] = c[i+1];
                c[i+1] = temp;
            }
        }
    }
    printf("\n\nElements of Array C are:\n");
    for(i=0; i<20; i++)
    {
        if(i==19)
            printf("%d", c[i]);
        else
            printf("%d, ", c[i]);
    }
    getch();
    return 0;
}

Here is the first snapshot of the sample run:

merge two arrays in ascending order c

Now supply any 10 array elements for array A and then 10 array elements for array B, press ENTER key to see three arrays. The first two array is the given array and then the third array will be the array that contains all the elements of array A and array B and arranged in ascending order. Here is the second snapshot:

merge arrays in ascending order c

The same concept is used to merge the two array in above program except that we have used some extra code to arrange the merged array in ascending order. To arrange each and every elements in ascending order

  • create a for loop starts from 0 to the size of the array
  • and inside the for loop, create another for loop with different loop variable starts from 0 to the size of the array
  • and inside the second for loop, compare each and every elements of the array
  • if the first array element is greater than the second one
  • then place the first one to the second and second to the first one
  • do the same things until all the elements gets arranged in ascending order

Allow User to Define Array Size

This program does the same job as of previous program, that is of merging two given arrays in ascending order. The only difference is, this program allows user to define the size of array too along with its elements:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[50], b[50], c[100], limitA, limitB, i, j, limitC, temp;
    printf("How many elements you want to store in array A: ");
    scanf("%d", &limitA);
    printf("How many elements you want to store in array B: ");
    scanf("%d", &limitB);
    printf("Enter %d elements in array A:", limitA);
    for(i=0; i<limitA; i++)
        scanf("%d", &a[i]);
    printf("Enter %d elements in array B:", limitB);
    for(i=0; i<limitB; i++)
        scanf("%d", &b[i]);
    printf("\nElements of Array A are:\n");
    for(i=0; i<limitA; i++)
    {
        if(i==(limitA-1))
            printf("%d", a[i]);
        else
            printf("%d, ", a[i]);
    }
    printf("\n\nElements of Array B are:\n");
    for(i=0; i<limitB; i++)
    {
        if(i==(limitB-1))
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
    for(i=0; i<limitA; i++)
        c[i] = a[i];
    for(j=0; j<limitB; j++)
    {
        c[i] = b[j];
        i++;
    }
    limitC = i;
    for(j=0; j<(limitC-1); j++)
    {
        for(i=0; i<(limitC-1); i++)
        {
            if(c[i]>c[i+1])
            {
                temp = c[i];
                c[i] = c[i+1];
                c[i+1] = temp;
            }
        }
    }
    printf("\n\nElements of Array C are:\n");
    for(i=0; i<limitC; i++)
    {
        if(i==(limitC-1))
            printf("%d", c[i]);
        else
            printf("%d, ", c[i]);
    }
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c merge two array in ascending order

Merge Two Arrays in Descending Order

Now let's create another program that will merge the two given arrays in descending order:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[10], b[10], c[20], i, j, limitC, temp;
    printf("Enter 10 elements in array A:");
    for(i=0; i<10; i++)
        scanf("%d", &a[i]);
    printf("Enter 10 elements in array B:");
    for(i=0; i<10; i++)
        scanf("%d", &b[i]);
    printf("\nElements of Array A are:\n");
    for(i=0; i<10; i++)
    {
        if(i==9)
            printf("%d", a[i]);
        else
            printf("%d, ", a[i]);
    }
    printf("\n\nElements of Array B are:\n");
    for(i=0; i<10; i++)
    {
        if(i==9)
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
	
    // merging the two arrays
    for(i=0; i<10; i++)
        c[i] = a[i];
    for(j=0; j<10; j++)
    {
        c[i] = b[j];
        i++;
    }
	
    // sorting the merged array
    for(j=0; j<19; j++)
    {
        for(i=0; i<19; i++)
        {
            if(c[i]<c[i+1])
            {
                temp = c[i];
                c[i] = c[i+1];
                c[i+1] = temp;
            }
        }
    }
    printf("\n\nElements of Array C are:\n");
    for(i=0; i<20; i++)
    {
        if(i==19)
            printf("%d", c[i]);
        else
            printf("%d, ", c[i]);
    }
    getch();
    return 0;
}

Below is the final snapshot of the sample run:

merge two arrays in descending order c

Now let's modify the above program and allow user to create the limit or size for both the array. The question is, write a program in C to read size and values of two array, merge elements of both array into third Array in descending order. Print all the array at last. The program given below is the answer to this question:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[50], b[50], c[100], limitA, limitB, i, j, limitC, temp;
    printf("How many elements you want to store in array A: ");
    scanf("%d", &limitA);
    printf("How many elements you want to store in array B: ");
    scanf("%d", &limitB);
    printf("Enter %d elements in array A:", limitA);
    for(i=0; i<limitA; i++)
        scanf("%d", &a[i]);
    printf("Enter %d elements in array B:", limitB);
    for(i=0; i<limitB; i++)
        scanf("%d", &b[i]);
    printf("\nElements of Array A are:\n");
    for(i=0; i<limitA; i++)
    {
        if(i==(limitA-1))
            printf("%d", a[i]);
        else
            printf("%d, ", a[i]);
    }
    printf("\n\nElements of Array B are:\n");
    for(i=0; i<limitB; i++)
    {
        if(i==(limitB-1))
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
    for(i=0; i<limitA; i++)
        c[i] = a[i];
    for(j=0; j<limitB; j++)
    {
        c[i] = b[j];
        i++;
    }
    limitC = i;
    for(j=0; j<(limitC-1); j++)
    {
        for(i=0; i<(limitC-1); i++)
        {
            if(c[i]<c[i+1])
            {
                temp = c[i];
                c[i] = c[i+1];
                c[i+1] = temp;
            }
        }
    }
    printf("\n\nElements of Array C are:\n");
    for(i=0; i<limitC; i++)
    {
        if(i==(limitC-1))
            printf("%d", c[i]);
        else
            printf("%d, ", c[i]);
    }
    getch();
    return 0;
}

This is the final snapshot of the sample run:

c merge two array in descending order

Same Program in Other Languages

C Online Test


« Previous Program Next Program »