merge two arrays in C
Code snippet on how to merge two arrays in C
#include<stdio.h>
int main(){
int a1[] = {1, 2, 3};
int a2[] = {4, 5, 6};
int a3[6];
int n1 = sizeof(a1)/sizeof(a1[0]);
int n2 = sizeof(a2)/sizeof(a2[0]);
int i,j,k;
//Merging the two arrays
for (i=0; i<n1; i++)
a3[i]=a1[i];
for (j=0; j<n2; j++)
a3[n1+j]=a2[j];
//Displaying the merged array
printf("Merged array: \n");
for (k=0; k<n1+n2; k++)
printf("%d ",a3[k]);
return 0;
}
This code is used to merge two arrays into a single array. The two arrays are a1 and a2. The two arrays are declared with fixed size and containing some integer values. The variable n1 and n2 is used to store the size of each array. The variable i, j and k are loop counters. The first loop is used to copy all elements of a1 into a3 while the second loop is used to copy all elements of a2 into a3. Finally, the merged array is printed out.