Help with arrays

This is a discussion on Help with arrays within the C++ Programming forums, part of the General Programming Boards category; 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] = ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    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.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Smile

    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]);
    }
    }

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    Smile

    Thank for the reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21