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
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the sizes of the two arrays, the arrays themselves, and the merged array.
- Input the Sizes of the Arrays: Use
scanfto take input from the user for the sizes of the two arrays. - Input the Elements of Both Arrays: Use loops to take input from the user for the elements of the two arrays.
- Merge the Arrays: Use a loop to copy elements from both arrays into the merged array.
- Display the Merged Array: Use
printfto 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
n1andn2are declared to store the sizes of the two arrays.n3stores the size of the merged array. The arraysarr1,arr2, andmergedare used to store the elements of the first array, second array, and the merged array, respectively.iandjare 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. Thescanffunction then reads the input and stores it in the variablen1.
Step 3: Declare the First Array
- The program declares an array
arr1of sizen1to hold the elements of the first array.
Step 4: Input the Elements of the First Array
- The program uses a
forloop to take input for each element of the first array. The loop iterates from 0 ton1-1, reading the elements usingscanf.
Step 5: Input the Size of the Second Array
- The program prompts the user to enter the size of the second array using
printf. Thescanffunction then reads the input and stores it in the variablen2.
Step 6: Declare the Second Array
- The program declares an array
arr2of sizen2to hold the elements of the second array.
Step 7: Input the Elements of the Second Array
- The program uses a
forloop to take input for each element of the second array. The loop iterates from 0 ton2-1, reading the elements usingscanf.
Step 8: Declare the Merged Array
- The program calculates the size of the merged array
n3as the sum ofn1andn2, and declares an arraymergedof sizen3to hold the combined elements of both arrays.
Step 9: Merge the First Array into the Merged Array
- The program uses a
forloop to copy each element ofarr1intomerged.
Step 10: Merge the Second Array into the Merged Array
- The program uses another
forloop to copy each element ofarr2intomerged, 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
forloop and theprintffunction.
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.