I need help on merging two arrays in one.
Here is the question: Write a function named concat with 4 args: an array of ints, the number of elements in that array, another array of ints, and the number of elements in the array. The function's job is to allocate and return an array that is the concatenation of the 2 args arrays.
FOR EXAMPLE: If your function passed {3,5} & {4,0,0,4}, then it should return {3,5,4,0,0,3}
HERE IS WHAT I HAVE SO FAR:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int concat(const int pass [], const int elements, const int pass2[], const int elements2);
int main()
{
const int pass [] = {3,7,3,11,2,5,6,4,7};
const int elements = 9;
const int pass2 [] = {3,5,6,7, 8};
const int elements2 = 4;
concat(pass, elements, pass2, elements2);
system("pause");
}
int concat(const int pass [], const int elements, const int pass2[], const int elements2)
{
static int count;
int number = 0;
int *copyelem = 0;
copyelem = (int *) malloc (elements *sizeof(int) + elements2 *sizeof(int));
for (count = 0; count < elements; count++)
{
copyelem[count] = pass[count];
printf("%i ", copyelem[count]);
}
printf(" ");
while( count < elements2)
{
copyelem[count] = pass2[count];
printf("%i ", copyelem[count]);
count++;
}
}
|
my problem is, it only outputs: 3,7,3,11,2,5,6,4,7
but I wanna output: 3,7,3,11,2,5,6,4,7,3,5,6,7, 8
PLEAASE HELP!!