Thread: switch from 2d array to 1d array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    switch from 2d array to 1d array

    i have a 2d array...W[9][9] and i want to put it a 1d array w_in1d[81]
    but it gives me the wrong values
    please help!

    Code:
    /*initialize W_in1d matrix*/
      for(i = 0; i < 81; i++){
          W_in2d[i]=0.0;
        }
    
     for(k = 0; k < 81; k++){
       for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            W_in1d[k]=W[i][j];
        }
    
    for (i = 0; i < 81; i++) {
     printf("%6.4lf ", W_in1d[i]);
      }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You've got too many loops.
    Code:
     for(k = 0; k < 81; k++){
       for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            W_in1d[k]=W[i][j];
        }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    well dont you need 3 loops?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Aren't you trying to do this?
    Code:
    #include <stdio.h>
    int main(void)
    {
       float b[9][9], a[(sizeof b / sizeof *b) * (sizeof *b / sizeof **b)];
       size_t i, j, k = 0;
    
       for ( i = 0; i < sizeof b / sizeof *b; i++ )
       {
          for ( j = 0; j < sizeof *b / sizeof **b; j++ )
          {
             b[i][j] = k++;
          }
       }
    
       k = 0;
       for ( i = 0; i < sizeof b / sizeof *b; i++ )
       {
          for ( j = 0; j < sizeof *b / sizeof **b; j++ )
          {
             a[k++] = b[i][j];
          }
       }
    
       for ( i = 0; i < sizeof a / sizeof *a; i++ )
       {
          printf("%6.4f ", a[i]);
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    no i allready have values in the 2d array and i want to transfer them to an empty 1d array

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Uh, that's what this is doing.
    Code:
       k = 0;
       for ( i = 0; i < sizeof b / sizeof *b; i++ )
       {
          for ( j = 0; j < sizeof *b / sizeof **b; j++ )
          {
             a[k++] = b[i][j];
          }
       }
    Or do you see a being assigned values somewhere that I don't?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    it says
    warning: comparison between signed and unsigned

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    and it changes the values in my array

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't suppose you could do what I did and post your code that actually demonstrates the issue?

    Quote Originally Posted by sass208
    it says
    warning: comparison between signed and unsigned
    Oh, wait. You didn't copy and paste all of my code, you only did it part way, right?
    Last edited by Dave_Sinkula; 12-09-2006 at 09:38 PM. Reason: Consecutive posts.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    OK great!!!
    ya i had put the first part of what u posted...i took it off and now it works !!! yay
    but it still has that warning

    warning: comparison between signed and unsigned

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    ok i fixed it up
    it runs perfectly!
    thank you soooooooooo much!

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Uh, where? With the i,j,k? I don't suppose you used the size_t that I did, and instead used int. That would be a signed/unsigned issue.
    Last edited by Dave_Sinkula; 12-09-2006 at 09:52 PM. Reason: D'oh! Pokey again. :(
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    hi man,

    Hey Im new to this programming language, so dont mind if I say something wrong ok.

    I think you can go with this code:
    Code:
    k=0;
    for (i=0;i<9;i++)
    {
    for (j=0;j<9;j++)
    {
    w_in1d[k]=w_in2d[i][j];
    k++;
    }
    }
    it will copy all your 2d elements to 1d

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. 1D and 2D Arrays
    By Rajin in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 06:23 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM