#include <stdio.
h> // Standard Input/Output library
int main() { // Starting the main function.
int a[50], b[50], merged[100], n1, n2, i;
// Asking user to input the size and elements of the first array.
printf("Enter the size of the first array: ");
scanf("%d", &n1);
printf("Enter %d elements for the first array:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &a[i]);
}
// Asking user to input the size and elements of the second array.
printf("Enter the size of the second array: ");
scanf("%d", &n2);
printf("Enter %d elements for the second array:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &b[i]);
}
// Merging the two arrays.
for(i = 0; i < n1; i++) {
merged[i] = a[i]; // First array elements.
}
for(i = 0; i < n2; i++) {
merged[n1+i] = b[i]; // Second array elements.
}
// Displaying the merged array.
printf("Merged array is:\n");
for(i = 0; i < n1 + n2; i++) {
printf("%d ", merged[i]);
}
return 0; // Ending the program.
}
OUTPUT:
* First array:
* Size: 3
* Elements: 10 20 30
* Second array:
* Size: 4
* Elements: 100 200 300 400
Enter the size of the first array: 3
Enter 3 elements for the first array:
10
20
30
Enter the size of the second array: 4
Enter 4 elements for the second array:
100
200
300
400
Merged array is:
10 20 30 100 200 300 400
Step-by-step trace for the example:
* n1 becomes 3. a becomes [10, 20, 30].
* n2 becomes 4. b becomes [100, 200, 300, 400].
* Merging:
* merged[0] = a[0] (10)
* merged[1] = a[1] (20)
* merged[2] = a[2] (30)
* merged[3+0] = b[0] (100)
* merged[3+1] = b[1] (200)
* merged[3+2] = b[2] (300)
* merged[3+3] = b[3] (400)
* So, merged array becomes [10, 20, 30, 100, 200, 300, 400].