# Help with arrays

• 02-22-2010
lilostitch382
Help with arrays
Hi

I have three array which is array1,array2 and array3 as shown below:

double array1[3] = "1.6,1.3,1.9"
double array2[5] = "2.1,3.6,4.6,8.2,9.9"
double array3[5];

I would like to extract two elements from array1 which is 1.6, 1.3 and three elements from array2 which is 2.1,3.6,4.6

the final extracted element will be concatenated which is "1.6,1.3,2.1,3.6,4.6" and assign to array3.

May I know what is the best way to do it?

Thank you.
• 02-22-2010
There isnt really any special way to do it, just copy the elements to array3. For example, to copy the first element of array1 to the first element of array3, do
Code:

`array3[0]=array1[0];`
To copy the remaining 4 elements, just do 4 similar lines, giving the appropriate source and destination indices, and source arrays.

If you plan on expanding this and start copying, say, 10 elements from array1, and 20 elements from array2, you'd probably want to do a "for" loop. Otherwise, manually copying the 5 elements one at a time (i.e. with 5 lines of code) is probably sufficient.

You could use other things like "memcpy", but I'm sure there isnt any practical benefit for your purposes.
• 02-22-2010
rekha_sri
You asked better way to copy the first two elements from the array1 and three elements from array2 and stored into array3.Following example better way for your requirement.
If you use this program you don't want to manually copy the elements from the array.

Code:

```#include <stdio.h> #include <string.h> int main () { int i; double array1[3] = {1.6,1.3,1.9}; double array2[5] = {2.1,3.6,4.6,8.2,9.9}; double array3[5]; memcpy (array3,array1,2*sizeof(double)); memcpy (array3+2,array2,3*sizeof(double)); for(i=0;i<5;i++) {   printf ("array3 : %.1f\n",array3[i]); } }```
• 02-24-2010
lilostitch382