Results 1 to 4 of 4

Thread: converting multidimensional arrays

  1. #1

    Thread Starter
    Lively Member Liquid Pennies's Avatar
    Join Date
    Jun 2002
    Location
    Charlotte, NC
    Posts
    124

    converting multidimensional arrays

    ok, now that i have my question about defining multidimensional arrays, how do i convert them. i really just want to know how to convert a 3d array into 2d, as well as a 2d array into a 1d. (im not going to bother converting 3d to 1d, b/c that would be too long of an array when i would rather deal with it in 2d)

    example

    2d to 1d


    visual basic code:--------------------------------------------------------------------------------
    dim MyArray(2,2)
    dim MyOneDArray(8)
    MyOneDArray(0) = MyArray(0,0)
    MyOneDArray(1) = MyArray(1,0)
    MyOneDArray(2) = MyArray(2,0)
    MyOneDArray(3) = MyArray(0,1)
    MyOneDArray(4) = MyArray(1,1)
    MyOneDArray(5) = MyArray(2,1)
    MyOneDArray(6) = MyArray(0,2)
    MyOneDArray(7) = MyArray(1,2)
    MyOneDArray(0) = MyArray(2,2)
    --------------------------------------------------------------------------------


    but, ya know, i want to work with a much larger array conversion, and id dont want to have to type all that out...

  2. #2
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    Use some multiplication and addition.

    VB Code:
    1. Const X_MAX As Integer = 10
    2. Const Y_MAX As Integer = 20
    3. Dim Array1(X_MAX * Y_MAX) As Integer
    4. Dim Array2(X_MAX, Y_MAX) As Integer
    5.  
    6. 'Convert it in a loop something like this.
    7. Array1(y * X_MAX + x) = Array2(x, y)

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    An n dimensional array is stored the same way. In practice you could use copymemory and varptrarray to just make the 1d array to point on the 2d array
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Add two listboxes List1 & List2 and try this.

    VB Code:
    1. Option Explicit
    2.  
    3. Private myArray() As Long
    4. Private myOneDArray() As Long
    5.  
    6. Private Sub Form_Load()
    7.     Dim i As Long, j As Long
    8.     ReDim myArray(2, 2)
    9.     For i = 0 To 2
    10.         For j = 0 To 2
    11.             myArray(i, j) = i
    12.             List1.AddItem myArray(i, j)
    13.         Next
    14.     Next
    15.     myOneDArray = make1D(myArray)
    16.     For i = 0 To UBound(myOneDArray)
    17.         List2.AddItem myOneDArray(i)
    18.     Next
    19. End Sub
    20.  
    21. Private Function make1D(ByRef x() As Long) As Long()
    22.     Dim retVal() As Long, i As Long, n As Long: ReDim retVal(2 ^ (UBound(x, 2) + 1))
    23.     For i = 0 To UBound(retVal)
    24.         retVal(i) = x(i \ (UBound(x, 2) + 1), i Mod (UBound(x, 2) + 1))
    25.     Next
    26.     make1D = retVal
    27. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width