Lets write a C program to merge two arrays into third array in alternative position.
Page Contents
Enter 5 elements for array a
10
12
14
16
18
Enter 5 elements for array b
11
13
15
17
19
Merging arrays a & b into c in alternate position
Array elements of c is:
10
11
12
13
14
15
16
17
18
19
Step 1: after first for loop
Step 2: after second for loop
Method 1: Array a and b are of same size
#include<stdio.h> #define N 5 #define M (N * 2) int main() { int a[N], b[N], c[M], i, k; printf("Enter %d elements for array a\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("Enter %d elements for array b\n", N); for(i = 0; i < N; i++) scanf("%d", &b[i]); printf("\nMerging arrays a & b into c in alternate position\n"); for(i = 0, k = 0; i < N; i++, k += 2) c[k] = a[i]; for(i = 0, k = 1; i < N; i++, k += 2) c[k] = b[i]; printf("Array elements of c is:\n"); for(i = 0; i < M; i++) printf("%d\n", c[i]); return 0; }
Output:
Enter 5 elements for array a
0
2
4
6
8
Enter 5 elements for array b
1
3
5
7
9
Merging arrays a & b into c in alternate position
Array elements of c is:
0
1
2
3
4
5
6
7
8
9
Here array variables a and b have same size. To copy the elements of array variable a to c, we initialize i to 0 and k to o, and start assigning values from a[i] to k[k], we increment the value of i by 1 for each iteration of for loop, but we increment the value of k by 2 for each iteration of first for loop. This way we assign elements of a to the values present at index 0, 2, 4, 6, 8 of array variable c.
In the next for loop, we reset the value of i to 0, and k value is rest to 1. k value keeps incrementing by 2 for each iteration of for loop, while i value increments by 1 for each iteration of the for loop. So the values present at index 0, 1, 2, 3, 4 of variable b are copied to the index places of 1, 3, 5, 7, 9 of array variable c.
Method 2: Array a and b are of different size
#include<stdio.h> #define N1 3 #define N2 8 #define M ( (N1 > N2) ? (N1 * 2) : (N2 * 2) ) int main() { int a[N1], b[N2], c[M] = {0}, i, k; printf("Enter %d elements for array a\n", N1); for(i = 0; i < N1; i++) scanf("%d", &a[i]); printf("Enter %d elements for array b\n", N2); for(i = 0; i < N2; i++) scanf("%d", &b[i]); printf("\nMerging arrays a & b into c in alternate position\n"); for(i = 0, k = 0; i < N1; i++, k += 2) c[k] = a[i]; for(i = 0, k = 1; i < N2; i++, k += 2) c[k] = b[i]; printf("Array elements of c is:\n"); for(i = 0; i < M; i++) printf("%d\n", c[i]); return 0; }
Output:
Enter 3 elements for array a
0
2
4
Enter 8 elements for array b
1
3
5
6
7
8
9
10
Merging arrays a & b into c in alternate position
Array elements of c is:
0
1
2
3
4
5
0
6
0
7
0
8
0
9
0
10
As you can see we’re using macros to assign size to the arrays a and b. You can change the size and play around to check different output for different input sizes.
N1 is the size of array a, N2 is the size of array b. Please execute this program and modify the values of N1 to be bigger than N2 and next change N2 to be bigger than N1, and then make both N1 and N2 equal – and check the outputs.
Inside macro M, which is the size of resultant array(c[M]), we store double the size of the biggest array we’re merging into c. We are doing this because, the sizes of the arrays to be merged might be different and the remaining positions will be filled with garbage values. So instead we’ll be pre-filling zeros in the places where there are no elements to fill.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert