Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MULTI-DIMENSIONAL ARRAYS SEEN AS ONE-DIMENSIONAL ONE

MULTI-DIMENSIONAL ARRAYS SEEN AS ONE-DIMENSIONAL ONE

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structures
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emrosa
    wrote on last edited by
    #1

    :confused: Hello. I know that a two-dimensional array can be handles as one-dimensional array by converting the indexes to a single one. Supose you have the following: int *table; table=new int table[3*3]; When referencig table[2][2] by a single index index=(2*3+2); then table[index] can be recalled. Now, I'm having a three-dimensional array and because is faster to handle dynamic arrays, I can define it as: int table1; table1=new int table[3*3*3]; but I got lost finding the index conversion formulae... :confused: Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

    A M 2 Replies Last reply
    0
    • E emrosa

      :confused: Hello. I know that a two-dimensional array can be handles as one-dimensional array by converting the indexes to a single one. Supose you have the following: int *table; table=new int table[3*3]; When referencig table[2][2] by a single index index=(2*3+2); then table[index] can be recalled. Now, I'm having a three-dimensional array and because is faster to handle dynamic arrays, I can define it as: int table1; table1=new int table[3*3*3]; but I got lost finding the index conversion formulae... :confused: Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

      A Offline
      A Offline
      Alan Chambers
      wrote on last edited by
      #2

      Your old formulae was xPos + (yPos x Height). I believe your new formulae would therefore be: (xPos + (yPos x Width)) + (zPos x (Height x Width)). This could probably be further reduced. "When I left you I was but the learner, now I am the master" - Darth Vader

      E 1 Reply Last reply
      0
      • E emrosa

        :confused: Hello. I know that a two-dimensional array can be handles as one-dimensional array by converting the indexes to a single one. Supose you have the following: int *table; table=new int table[3*3]; When referencig table[2][2] by a single index index=(2*3+2); then table[index] can be recalled. Now, I'm having a three-dimensional array and because is faster to handle dynamic arrays, I can define it as: int table1; table1=new int table[3*3*3]; but I got lost finding the index conversion formulae... :confused: Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

        M Offline
        M Offline
        Martin Ziacek
        wrote on last edited by
        #3

        Try this: int d1 = 3; int d2 = 4; int d3 = 5; BYTE x = 0; BYTE *b = new BYTE[d1*d2*d3]; for (int i = 0; i < d1; i++) { for (int j = 0; j < d2; j++) { for (int k = 0; k < d3; k++) { b[i*(d2*d3) + j*d3 + k] = x; x++; } } } I hope it does what you need.

        E 1 Reply Last reply
        0
        • A Alan Chambers

          Your old formulae was xPos + (yPos x Height). I believe your new formulae would therefore be: (xPos + (yPos x Width)) + (zPos x (Height x Width)). This could probably be further reduced. "When I left you I was but the learner, now I am the master" - Darth Vader

          E Offline
          E Offline
          emrosa
          wrote on last edited by
          #4

          Thanks for your help....I tried that but it did not work...however, you gave an idea and I put myself to think deeply and I just found this: Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in. If MArray is declared as int MArray[max1stdim*max2nddim*...*maxnthdim]; then the index referencing the location (i1,i2,...in-1,in) will be: index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be: index=(i*4*3)+(j*2)+k I have tried this and it works! Thanks again..... :) Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

          A 1 Reply Last reply
          0
          • M Martin Ziacek

            Try this: int d1 = 3; int d2 = 4; int d3 = 5; BYTE x = 0; BYTE *b = new BYTE[d1*d2*d3]; for (int i = 0; i < d1; i++) { for (int j = 0; j < d2; j++) { for (int k = 0; k < d3; k++) { b[i*(d2*d3) + j*d3 + k] = x; x++; } } } I hope it does what you need.

            E Offline
            E Offline
            emrosa
            wrote on last edited by
            #5

            Thanks for your help....I have tried that and it did work...therefore, making a generalization for this, I just found this: Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in. If MArray is declared as int MArray[max1stdim*max2nddim*...*maxnthdim]; then the index referencing the location (i1,i2,...in-1,in) will be: index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be: index=(i*4*3)+(j*2)+k I have tried this and it works! Thanks again..... :) ;) Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

            M 1 Reply Last reply
            0
            • E emrosa

              Thanks for your help....I have tried that and it did work...therefore, making a generalization for this, I just found this: Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in. If MArray is declared as int MArray[max1stdim*max2nddim*...*maxnthdim]; then the index referencing the location (i1,i2,...in-1,in) will be: index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be: index=(i*4*3)+(j*2)+k I have tried this and it works! Thanks again..... :) ;) Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

              M Offline
              M Offline
              Martin Ziacek
              wrote on last edited by
              #6

              Would be nice if you will write article about it and compare performance between different types of allocations and accesseing that memory.

              E 1 Reply Last reply
              0
              • M Martin Ziacek

                Would be nice if you will write article about it and compare performance between different types of allocations and accesseing that memory.

                E Offline
                E Offline
                emrosa
                wrote on last edited by
                #7

                How will I do that? What allocation and accesing methods?. Where will I submit the article?...Would you like to help me? Thanks, Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

                M 1 Reply Last reply
                0
                • E emrosa

                  How will I do that? What allocation and accesing methods?. Where will I submit the article?...Would you like to help me? Thanks, Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

                  M Offline
                  M Offline
                  Martin Ziacek
                  wrote on last edited by
                  #8

                  I understood you think addressing multi-dimensional array with one index you are computing by yourself is a faster method than addressing multi-dimensional array with pointers. So what I think is a demonstration program using two arrays of the same size but different access methods and allocation and comparison between speeds. And obviously, you will submit article with that demo program here at CP.

                  1 Reply Last reply
                  0
                  • E emrosa

                    Thanks for your help....I tried that but it did not work...however, you gave an idea and I put myself to think deeply and I just found this: Let be a multidimensional array MArray[max1stdim][max2nddim]...[maxnthdim], the indexes referencing certain location be i1,i2,...,in. If MArray is declared as int MArray[max1stdim*max2nddim*...*maxnthdim]; then the index referencing the location (i1,i2,...in-1,in) will be: index=(i1*max2nddim*max3rddim*...*maxnthdim)+(i2*max3rddim*...*maxnthdim)+...+(in-1)*maxnthdim+in Then for an array MArray[4][3][2] declared as MArray[3*3*3], the single index to reference (i,j,k)will be: index=(i*4*3)+(j*2)+k I have tried this and it works! Thanks again..... :) Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

                    A Offline
                    A Offline
                    Alan Chambers
                    wrote on last edited by
                    #9

                    Can ask one question? how can you have "an array MArray[4][3][2] declared as MArray[3*3*3]"? If you are trying to access the 3*3*3 MArray at position [4][3][2] using a single index then that is impossible, because the rows and columns will be indexed from 0 through to 2 for each x,y and z column, so 4 and 3 are invalid index positions. If you were trying to access the last record in a 3*3*3 matrix, the single index value you require is 26 (0 to 8, 9 to 17, 18 to 26). Using my equation earlier, if you were trying to index the last position in the 3d array, so XPos = 2, YPos = 2 and ZPos = 2, you would get: (2 + (2 * 3)) + (2 * 3 * 3)) = 26. the index position you desire. Martin has implemented the coded version of the equation (but in the opposite direction to the way I wrote my equation) :) : b[i*(d2*d3) + j*d3 + k] = x; i = ZPos j = YPos k = XPos d2 = Height d3 = Width *Please note I made a small error before when I wrote (yPos + Height), this is probably why you couldn't get it to work, it should have read (yPos + Width), which I have now changed it to. Without this correction the formula would only *work* if the 3d array was a cube. It should now work regardless of dimensions. Alan. "When I left you I was but the learner, now I am the master" - Darth Vader

                    L 1 Reply Last reply
                    0
                    • A Alan Chambers

                      Can ask one question? how can you have "an array MArray[4][3][2] declared as MArray[3*3*3]"? If you are trying to access the 3*3*3 MArray at position [4][3][2] using a single index then that is impossible, because the rows and columns will be indexed from 0 through to 2 for each x,y and z column, so 4 and 3 are invalid index positions. If you were trying to access the last record in a 3*3*3 matrix, the single index value you require is 26 (0 to 8, 9 to 17, 18 to 26). Using my equation earlier, if you were trying to index the last position in the 3d array, so XPos = 2, YPos = 2 and ZPos = 2, you would get: (2 + (2 * 3)) + (2 * 3 * 3)) = 26. the index position you desire. Martin has implemented the coded version of the equation (but in the opposite direction to the way I wrote my equation) :) : b[i*(d2*d3) + j*d3 + k] = x; i = ZPos j = YPos k = XPos d2 = Height d3 = Width *Please note I made a small error before when I wrote (yPos + Height), this is probably why you couldn't get it to work, it should have read (yPos + Width), which I have now changed it to. Without this correction the formula would only *work* if the 3d array was a cube. It should now work regardless of dimensions. Alan. "When I left you I was but the learner, now I am the master" - Darth Vader

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Sorry for the mistake...the dimensions of the array MArray[4][3][2] declared as MAarray[4*3*2]...and trying to access element Marray(3,3,2).... The rest of the formula is correct and your correction as well... Thanks,

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups