C Program to Merge Two Arrays

C Program to Merge Two Arrays

Merging two arrays is a common task in C programming. It allows you to combine elements from separate arrays into a single array, which is useful for managing datasets, sorting, or performing further operations. Understanding how to merge arrays teaches important concepts such as array indexing, memory allocation, and loops. In this tutorial, we will explore how to merge two arrays step by step with fully explained code examples.

Merging arrays involves copying the elements of the first array into a new array and then appending the elements of the second array immediately after. This approach preserves the order of elements in both arrays and gives you a combined array that contains all the elements from the original arrays.

Merging Two Arrays Using Loops

The most straightforward way to merge two arrays is by using loops to copy each element from the original arrays into a new array.

#include <stdio.h>

int main() {

    int n1, n2, i, j;

    printf("Enter the number of elements in the first array: ");
    scanf("%d", &n1);

    int arr1[50], arr2[50], merged[100];

    printf("Enter %d elements for the first array: ", n1);

    for(i = 0; i < n1; i++) {
        scanf("%d", &arr1[i]);
    }

    printf("Enter the number of elements in the second array: ");
    scanf("%d", &n2);

    printf("Enter %d elements for the second array: ", n2);

    for(i = 0; i < n2; i++) {
        scanf("%d", &arr2[i]);
    }

    // Copy first array into merged array
    for(i = 0; i < n1; i++) {
        merged[i] = arr1[i];
    }

    // Copy second array into merged array
    for(j = 0; j < n2; j++) {
        merged[i + j] = arr2[j];
    }

    printf("Merged array: ");

    for(i = 0; i < n1 + n2; i++) {
        printf("%d ", merged[i]);
    }

    printf("\n");

    return 0;

}

In this program, we first take the sizes and elements of the two arrays from the user. We then use two loops: the first loop copies elements of the first array into the merged array, and the second loop appends elements of the second array. Finally, we print the merged array.

Merging Arrays Using memcpy

For efficiency, especially with larger arrays, the memcpy function from the C standard library can be used to copy memory blocks directly instead of manually looping.

#include <stdio.h>
#include <string.h>

int main() {

    int n1, n2;

    printf("Enter the number of elements in the first array: ");
    scanf("%d", &n1);

    int arr1[50], arr2[50], merged[100];

    printf("Enter %d elements for the first array: ", n1);

    for(int i = 0; i < n1; i++) {
        scanf("%d", &arr1[i]);
    }

    printf("Enter the number of elements in the second array: ");
    scanf("%d", &n2);

    printf("Enter %d elements for the second array: ", n2);

    for(int i = 0; i < n2; i++) {
        scanf("%d", &arr2[i]);
    }

    // Use memcpy to copy arrays into merged array
    memcpy(merged, arr1, n1 * sizeof(int));
    memcpy(merged + n1, arr2, n2 * sizeof(int));

    printf("Merged array: ");

    for(int i = 0; i < n1 + n2; i++) {
        printf("%d ", merged[i]);
    }

    printf("\n");

    return 0;

}

Using memcpy, we copy the memory of arr1 into the merged array first, and then copy arr2 starting at the position after the last element of arr1. This approach is faster and reduces the risk of loop errors, especially for large arrays.

FAQs

Q1: Can I merge arrays of different types, like int and float?
No, arrays must be of the same type to merge directly. Mixing types requires type casting or using a more flexible data structure like struct or dynamic memory allocation.

Q2: What is the advantage of using memcpy over loops?
memcpy is faster because it copies memory in blocks rather than element by element, which is especially efficient for large arrays.

Q3: How can I merge arrays in descending order?
First merge the arrays using any method, then sort the merged array in descending order using a sorting algorithm like bubble sort or qsort.

Q4: What happens if the merged array size is smaller than the sum of the two arrays?
This leads to undefined behavior and memory corruption. Always ensure the merged array has enough space to hold all elements.

Conclusion

Merging two arrays is a fundamental skill in C programming. Whether you use loops or memcpy, understanding the process helps you manage data efficiently and prepares you for more complex tasks, such as sorting and searching within combined datasets. Practicing array merging improves your confidence in array manipulation and memory management.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: Merge Two Arrays in C – Step-by-step guide.
  3. TutorialsPoint: Arrays in C – In-depth array explanations.
  4. Programiz: C Programming Examples – Beginner-friendly examples.
Scroll to Top