|
-
Jul 14th, 2002, 06:24 PM
#1
Thread Starter
Lively Member
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...
-
Jul 14th, 2002, 07:58 PM
#2
Addicted Member
Use some multiplication and addition.
VB Code:
Const X_MAX As Integer = 10
Const Y_MAX As Integer = 20
Dim Array1(X_MAX * Y_MAX) As Integer
Dim Array2(X_MAX, Y_MAX) As Integer
'Convert it in a loop something like this.
Array1(y * X_MAX + x) = Array2(x, y)
-
Jul 15th, 2002, 02:24 AM
#3
transcendental analytic
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.
-
Jul 15th, 2002, 04:08 AM
#4
Retired VBF Adm1nistrator
Add two listboxes List1 & List2 and try this.
VB Code:
Option Explicit
Private myArray() As Long
Private myOneDArray() As Long
Private Sub Form_Load()
Dim i As Long, j As Long
ReDim myArray(2, 2)
For i = 0 To 2
For j = 0 To 2
myArray(i, j) = i
List1.AddItem myArray(i, j)
Next
Next
myOneDArray = make1D(myArray)
For i = 0 To UBound(myOneDArray)
List2.AddItem myOneDArray(i)
Next
End Sub
Private Function make1D(ByRef x() As Long) As Long()
Dim retVal() As Long, i As Long, n As Long: ReDim retVal(2 ^ (UBound(x, 2) + 1))
For i = 0 To UBound(retVal)
retVal(i) = x(i \ (UBound(x, 2) + 1), i Mod (UBound(x, 2) + 1))
Next
make1D = retVal
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|