Thread: How to merge 2 arrays?

1. How to merge 2 arrays?

I want to merge array 1 into 2 how can i do that?

2. Array A: The array that will receive array B
Array B: Array that we'll add to array A.

Be sure that A have enough space to merge with the B.
Loop until the last element of A and from there, start looping B too and copying from B to A, until the last element of B.

3. Make an array that has enough space, then start inserting an element from array 1 and array 2 into the new array, and the way you will insert the elements depends on what do you mean by merge, and that's acheived by using loops, and counters to controll the insertion.

4. Code:
```
#include<stdio.h>

#define ARRAY_LENGTH                       7
#define ARRAY_B_LENGTH                   5
#define ARRAY_A_LENGTH                 (ARRAY_B_LENGTH + ARRAY_LENGTH)

int main()
{
int array_a[ARRAY_A_LENGTH];
int array_b[ARRAY_B_LENGTH];
int i, j;

/* initialize or put value in arrays*/
for(i = 0, j = 47; i < ARRAY_LENGTH; i++)
array_a[i] = i + j;

for(i = 0, j = 53; i < ARRAY_B_LENGTH; i++)
array_b[i] = i + j;

/* Display all value of */
for(i = 0; i < ARRAY_LENGTH; i++)
printf("array_a[%d]=%d\n", i, array_a[i]);

for(i = 0; i < ARRAY_B_LENGTH; i++)
printf("array_b[%d]%d\n", i, array_b[i]);

/*Marge small one with the big*/
for(i = ARRAY_LENGTH, j = 0; i < ARRAY_A_LENGTH; i++, j++)
array_a[i] = array_b[j];

/* Display marged array */
for(i = 0; i < ARRAY_A_LENGTH; i++)
printf("array_a[%d] = %d\n",i, array_a[i]);

return 0;

}```
May get an idea .. .. instead of using two arrays, you may try with third array.