C Program to Merge Two Arrays

Introduction

Merging two arrays involves combining the elements of both arrays into a single array. This guide will show you how to write a C program to merge two arrays provided by the user.

Problem Statement

Create a C program that:

  • Takes the sizes of two arrays as input from the user.
  • Takes the elements of both arrays as input.
  • Merges the two arrays into a single array.
  • Displays the merged array.

Example:

  • Input: Array1 = [1, 3, 5], Array2 = [2, 4, 6]
  • Output: Merged Array = [1, 3, 5, 2, 4, 6]

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the sizes of the two arrays, the arrays themselves, and the merged array.
  4. Input the Sizes of the Arrays: Use scanf to take input from the user for the sizes of the two arrays.
  5. Input the Elements of Both Arrays: Use loops to take input from the user for the elements of the two arrays.
  6. Merge the Arrays: Use a loop to copy elements from both arrays into the merged array.
  7. Display the Merged Array: Use printf to display the merged array.

C Program

#include <stdio.h>

/**
 * C Program to Merge Two Arrays
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the sizes of the arrays and the arrays themselves
    int n1, n2, n3, i, j;

    // Step 2: Prompt the user to enter the size of the first array
    printf("Enter the number of elements in the first array: ");
    scanf("%d", &n1);

    // Step 3: Declare the first array
    int arr1[n1];

    // Step 4: Input the elements of the first array
    printf("Enter %d elements of the first array:\n", n1);
    for (i = 0; i < n1; i++) {
        scanf("%d", &arr1[i]);
    }

    // Step 5: Prompt the user to enter the size of the second array
    printf("Enter the number of elements in the second array: ");
    scanf("%d", &n2);

    // Step 6: Declare the second array
    int arr2[n2];

    // Step 7: Input the elements of the second array
    printf("Enter %d elements of the second array:\n", n2);
    for (i = 0; i < n2; i++) {
        scanf("%d", &arr2[i]);
    }

    // Step 8: Declare the merged array
    n3 = n1 + n2;
    int merged[n3];

    // Step 9: Merge the first array into the merged array
    for (i = 0; i < n1; i++) {
        merged[i] = arr1[i];
    }

    // Step 10: Merge the second array into the merged array
    for (j = 0; j < n2; j++) {
        merged[i + j] = arr2[j];
    }

    // Step 11: Display the merged array
    printf("Merged array: ");
    for (i = 0; i < n3; i++) {
        printf("%d ", merged[i]);
    }
    printf("\n");

    return 0;  // Step 12: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables n1 and n2 are declared to store the sizes of the two arrays. n3 stores the size of the merged array. The arrays arr1, arr2, and merged are used to store the elements of the first array, second array, and the merged array, respectively. i and j are loop counters.

Step 2: Input the Size of the First Array

  • The program prompts the user to enter the size of the first array using printf. The scanf function then reads the input and stores it in the variable n1.

Step 3: Declare the First Array

  • The program declares an array arr1 of size n1 to hold the elements of the first array.

Step 4: Input the Elements of the First Array

  • The program uses a for loop to take input for each element of the first array. The loop iterates from 0 to n1-1, reading the elements using scanf.

Step 5: Input the Size of the Second Array

  • The program prompts the user to enter the size of the second array using printf. The scanf function then reads the input and stores it in the variable n2.

Step 6: Declare the Second Array

  • The program declares an array arr2 of size n2 to hold the elements of the second array.

Step 7: Input the Elements of the Second Array

  • The program uses a for loop to take input for each element of the second array. The loop iterates from 0 to n2-1, reading the elements using scanf.

Step 8: Declare the Merged Array

  • The program calculates the size of the merged array n3 as the sum of n1 and n2, and declares an array merged of size n3 to hold the combined elements of both arrays.

Step 9: Merge the First Array into the Merged Array

  • The program uses a for loop to copy each element of arr1 into merged.

Step 10: Merge the Second Array into the Merged Array

  • The program uses another for loop to copy each element of arr2 into merged, starting from the position where the first array ended.

Step 11: Display the Merged Array

  • After merging both arrays, the program displays the merged array using a for loop and the printf function.

Step 12: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of elements in the first array: 3
Enter 3 elements of the first array:
1
3
5
Enter the number of elements in the second array: 3
Enter 3 elements of the second array:
2
4
6
Merged array: 1 3 5 2 4 6

Another Example:

Enter the number of elements in the first array: 4
Enter 4 elements of the first array:
10
20
30
40
Enter the number of elements in the second array: 2
Enter 2 elements of the second array:
5
15
Merged array: 10 20 30 40 5 15

Conclusion

This C program demonstrates how to merge two arrays into a single array. It covers basic concepts such as arrays, loops, and array manipulation, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top