Merge Two Arrays in C Programming Language

program-to-merge-two-arrays-in-c-language

Program Description:

In our previous article, We have looked at the program to Delete All duplicate elements in a sorted array. In today’s article, We are going to write a Program to Merge Two Arrays in C programming language. The program should accept two arrays from the user and merge them.

📢 This Program is part of the Array Practice Programs Collection.

Here are the excepted Input and Output of the program

Example Input and Output:

Input:

Enter desired size for array1 (1-100): 5

Please enter array elements(5) : 12 34 74 89 21

Enter desired size for array2 (1-100): 8

Please enter array elements(8) : 100 200 300 600 700 800 900 1000

Output:

Array-1: 12 34 74 89 21

Array-2: 100 200 300 600 700 800 900 1000

Merged Array: 12 34 74 89 21 100 200 300 600 700 800 900 1000

As you can see from the above example input and output, The program prompts for the Array-1 Size and Array-1 elements, Then Array-2 size and elements, Finally it merges both arrays and prints the results on the console.

Prerequisites:

We need to know the C Arrays and Functions to better understand the following program.

Algorithm to Merge Two Arrays in C:

First, we need to take the two input arrays from the user, Let the two input arrays are arr1 and arr2. and Then We need to create a third array which should be large enough to hold the arr1 and arr2. Let’s say the third array name as the mergedArr.

To Merge the two arrays, We will go through the all elements of the first array ( arr1) and copy them the mergedArr element by element using a For Loop. Once the first array is copied to mergedArr. Then we will go through the second input array( arr2), and Appends the arr2 elements to the mergedArr using another For Loop.

Once the above two for loops are completed, the mergedArr will contain the Merged Array.

Merge Two Arrays in C Program Explanation:

Here is a step-by-step explanation of the program.

  1. Declare two input arrays with the max size of 100, They are arr1[100] and arr2[100]. Also, declare another array that is going to hold the merged array, Call the third array as the mergedArr, The mergedArrsize is 200 ( Sum of both arr1 and arr2 sizes).
  2. We defined two helper functions, which are read() and display() functions. These functions are useful to read and print arrays.
    • The read() function takes an array as formal arguments and reads the elements from the user and updates the given array.
    • The display() function used to print the elements of the given array.
  3. Now, We need to take the input arrays ( arr1 and arr2) sizes and elements from the user.
  4. So prompt the user to provide size for the arr1, Then call the read() function with the arr1 and size as the actual arguments. – read(arr1, size);. This function call to read() function updates the arr1 with user values.
  5. Repeat the above step-3 and step-4 steps for the arr2 to get the arr2size and elements from the user.
  6. We got the two input arrays from the user. Display the arr1 and arr2 by using the display() function. So make two calls to dispaly() function from the main() function and provide the arr1 details for one function call, arr2 details for another function call.
    • display(arr1, size1);
    • display(arr2, size2);
  7. To Merge the two arrays, Use two for loops and go through the elements of the both arr1 and arr2 and copy them to mergedArr array.
  8. We will traverse through all elements of the arr1 using a For Loop and copy all elements to the mergedArr.
    • Start a For Loop from i=0 and go till i<size1
    • Copy the arr1[i] element to mergedArr[i] – mergedArr[i] = arr1[i];
  9. Similarly, Go through the elements of the arr2 and copy them to mergedArr.
    • Start a For Loop with (i = size1, j = 0) and go till the (i < (size1+size2) && j<size2)
    • Copy the arr2[i] to mergedArr[i] element – mergedArr[i] = arr2[j];
  10. Display the Merged Array on the console by calling the display() function – display(mergedArr, size1+size2);

Program to Merge Two Arrays in C Language:

Let’s convert the above Algorithm in the C Program – Here is the program to Merge Two Arrays in C Programming Langauge.

Program Output:

Compile and Run the Program.

To compile the program use the following command.

gcc merge-two-arrays.c

The above command, Generates an executable file named a.out. Run the executable file.

Test 1:

merge-two-arrays-in-c-program-output

As we can see from the above output, The program is properly merging the two input arrays and creating the new one.

For example, When we entered the two arrays 1 2 3 4 5 6 7 8 9 10 and 55 66 77 88 99, Then the program merged the arrays and provided the 1 2 3 4 5 6 7 8 9 10 55 66 77 88 99 array as output.

Test 2: Negative Test Case:

When a user enters an invalid size.

If the user enters the wrong size for any of the input arrays ( arr1 or arr2), Then the program is displaying an error message( Invalid Size, Max Array size reached, Try again).

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] have covered the Merging of Two unsorted arrays in our earlier articles. In today’s article, We will look at a Program to Merge Two Sorted Arrays […]

Leave a Reply