Thread: extracting columns from arrays

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    extracting columns from arrays

    Hi there, new to C here. I can't find information anywhere on how I could extract say a 2nd column from a 2D array and save it as a 1D array. Many thanks if you could help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not built-in, like you would get from an array language. You'll have to do it yourself. I would suggest a for loop.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    if I have a 2D array x[m][n], then why doesn't this work:

    for (m=0; m <= NR-1; m++);
    x_values[m] = x[m][0]; (storing column 1 in a separate array)

    it compiles just fine, but printed elements of an array don't make sense.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kamasin View Post
    if I have a 2D array x[m][n], then why doesn't this work:

    for (m=0; m <= NR-1; m++);
    x_values[m] = x[m][0]; (storing column 1 in a separate array)

    it compiles just fine, but printed elements of an array don't make sense.
    Also, why do you have <= SIZE-1 when < SIZE would work?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    6
    all array elements start with 0. So if you declare # of rows as 50, you would need to do the iteration only 49 times.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kamasin View Post
    all array elements start with 0. So if you declare # of rows as 50, you would need to do the iteration only 49 times.
    This is of course false. 0 through 49 is still 50 loops.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    6
    my bad, if you declare # of rows as 50, and begin with row = 0, you would need to do the for loop until row = 49, right?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kamasin View Post
    my bad, if you declare # of rows as 50, and begin with row = 0, you would need to do the for loop until row = 49, right?
    No. You would loop until it reached 50, at which point the test would fail, so it wouldn't execute the body of the loop for x == 50.
    Code:
    for( x = 0; x < 50; x++ )
        printf( "x is %d\n", x ) /*Would print 0 ... 49*/
    printf( "x is %d\n", x ); /*Would print x is 50*/

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would do [49] but not [50], yes.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    6
    what about the original question

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kamasin View Post
    Hi there, new to C here. I can't find information anywhere on how I could extract say a 2nd column from a 2D array and save it as a 1D array. Many thanks if you could help.
    Set the colum number and Use a for() loop... Like they told you.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kamasin View Post
    what about the original question
    Did you fix your for loop yet?

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    6
    for (m=0; m <= 999; m++);
    n=2;
    B[m] = A[m][n];
    printf("new array %f \n", B[m]);
    can't figure out why this doesn't work

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you ever see a semi-colon at the end of a line with a for-statement on it, then you know you have screwed up.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    If you ever see a semi-colon at the end of a line with a for-statement on it, then you know you have screwed up.
    I guess making it big and bright red wasn't enough to clue him in.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Importing from 2 columns to seperate arrays
    By tomeatworld in forum C Programming
    Replies: 5
    Last Post: 12-04-2010, 10:29 AM
  2. Columns in a console out...
    By thecoswen in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2010, 04:27 PM
  3. aes mix columns
    By zxcv in forum C Programming
    Replies: 8
    Last Post: 01-04-2008, 03:28 PM
  4. Output in columns
    By stryker1080 in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2005, 12:58 PM
  5. Blocking columns
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-02-2002, 05:27 PM