Introduction
Merging two arrays involves combining the elements of two arrays into a single array. In C, you can use pointers to efficiently traverse the two arrays and copy their elements into a third array. This guide will show you how to write a C program to merge two arrays using pointers.
Example:
- Input: Array 1
[1, 3, 5], Array 2[2, 4, 6] - Output: Merged array
[1, 2, 3, 4, 5, 6]
Problem Statement
Create a C program that:
- Takes two arrays of integers as input from the user.
- Uses pointers to merge the two arrays into a third array.
- Displays the merged array.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Declare the Arrays and Pointer Variables: Declare integer arrays to store the elements and pointers for accessing and manipulating the elements.
- Input the Array Elements: Use loops to take input for the elements of the two arrays from the user.
- Merge the Arrays Using Pointers: Use pointers to copy the elements of the two arrays into a third array.
- Display the Merged Array: Use
printfto display the merged array.
C Program to Merge Two Arrays Using Pointers
#include <stdio.h>
int main() {
// Step 2: Declare the arrays and pointer variables
int arr1[100], arr2[100], merged[200];
int n1, n2, n3, i;
int *ptr1, *ptr2, *ptr_merged;
// Step 3: Input the number of elements and the array elements for the first array
printf("Enter the number of elements in the first array: ");
scanf("%d", &n1);
printf("Enter the elements of the first array: ");
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
// Input the number of elements and the array elements for the second array
printf("Enter the number of elements in the second array: ");
scanf("%d", &n2);
printf("Enter the elements of the second array: ");
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Step 4: Merge the arrays using pointers
ptr1 = arr1; // Pointer to the first array
ptr2 = arr2; // Pointer to the second array
ptr_merged = merged; // Pointer to the merged array
// Copy elements from the first array to the merged array
for (i = 0; i < n1; i++) {
*ptr_merged = *ptr1;
ptr1++;
ptr_merged++;
}
// Copy elements from the second array to the merged array
for (i = 0; i < n2; i++) {
*ptr_merged = *ptr2;
ptr2++;
ptr_merged++;
}
n3 = n1 + n2; // Total number of elements in the merged array
// Step 5: Display the merged array
printf("Merged array: ");
for (i = 0; i < n3; i++) {
printf("%d ", merged[i]);
}
printf("\n");
return 0; // Return 0 to indicate successful execution
}
Explanation
Step 2: Declare the Arrays and Pointer Variables
- The integer arrays
arr1andarr2are declared to store the elements of the two input arrays. - The array
mergedis declared to store the elements of the merged array. - The pointers
ptr1,ptr2, andptr_mergedare used to traverse and copy the elements from the two input arrays to the merged array.
Step 3: Input the Array Elements
- The program prompts the user to enter the number of elements in the first array and stores this value in
n1. - A
forloop is used to take input for each element of the first array. - Similarly, the program prompts the user to enter the number of elements in the second array and stores this value in
n2. - Another
forloop is used to take input for each element of the second array.
Step 4: Merge the Arrays Using Pointers
- The pointer
ptr1is initialized to point to the first element of the first array (arr1), andptr2is initialized to point to the first element of the second array (arr2). - The pointer
ptr_mergedis initialized to point to the first element of the merged array (merged). - A
forloop is used to copy each element from the first array into the merged array using the pointersptr1andptr_merged. - Another
forloop is used to copy each element from the second array into the merged array using the pointersptr2andptr_merged. - The total number of elements in the merged array is stored in
n3.
Step 5: Display the Merged Array
- The program uses a
forloop to display the elements of the merged array.
Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example Output:
Enter the number of elements in the first array: 3
Enter the elements of the first array: 1 3 5
Enter the number of elements in the second array: 3
Enter the elements of the second array: 2 4 6
Merged array: 1 3 5 2 4 6
Conclusion
This C program demonstrates how to merge two arrays using pointers. It covers basic concepts such as pointer manipulation, array traversal, and array merging, making it a useful example for beginners learning C programming.