Convert a two-dimensional array to a one demensional array

This is a discussion on Convert a two-dimensional array to a one demensional array within the C Programming forums, part of the General Programming Boards category; right it should be Code: fint i, j, k; int power1d[56]; for(i = 0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) ...

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    right it should be
    Code:
    fint i, j, k;
    int power1d[56];
    
    for(i = 0; i<NROWS; i++)
    {
    for(j=0; j<NCOLS; j++)
    {
    for(k=0; k <56; k++)
    power1d[k] = power[i][j];
    }}

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i fixed the power1d

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Closer. You just have to decide what k is going to be. Currently you are assigning each element of power into every element of power1d, which means only the last element is the one you get kept. You're already in the for loop, so you're looking at one specific element of the power array -- now where does that one specific element need to go?

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    Yea i knew something was wrong because when i printed it out it was a mess, so does k need to be an outside loop?

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    k shouldn't be a loop at all. k needs to be computed based on i and j.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so k should be
    k=(i +1) *(j+1) - 1
    ????

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i dont think this is the right equation? it works for the first seven then messes up on the index, but the value of the index is correct, do you know of an equation that would fix this?

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    figured it out
    k=(i*7)+j;

  9. #24
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by belkins View Post
    figured it out
    k=(i*7)+j;
    bingo!

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Look at code that I have posted. I have given many examples of how to do this.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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