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. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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