# transfering contents of 3 arrays to 1

This is a discussion on transfering contents of 3 arrays to 1 within the C Programming forums, part of the General Programming Boards category; I have 3 arrays that each contain 5 numbers between 0 and 6. The total non-0 numbers for all 3 ...

1. ## transfering contents of 3 arrays to 1

I have 3 arrays that each contain 5 numbers between 0 and 6. The total non-0 numbers for all 3 arrays is 5. I want to put those 5 non-0 numbers into a single array. Any idea how I would do it? At the moment I'm basing my approach on the simple method of copying one array to another:
Code:
for (i=0; i<5; i++)
{
finalarray[i]=rdarray[i];
}
but I don't seem to be getting anywhere. Thanks

2. Something along the lines of the following should work. There are more elegant ways, but this was the first thing that came to mind. Make sure that the final array is big enough too.
Code:
for ( i = 0; i < 5; i++ ) {
if ( array1[i] != 0 ) final[x++] = array1[i];
if ( array2[i] != 0 ) final[x++] = array2[i];
if ( array3[i] != 0 ) final[x++] = array3[i];
}
-Prelude