Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 10:14:49 AM

Login with username, password and session length
Search:     Advanced search
* Home Help Search Calendar Links Login Register
+  Machsupport Forum
|-+  Mach Discussion
| |-+  VB and the development of wizards
| | |-+  Multidimensional arrays in Cypress Basic?
Pages: 1   Go Down
Print
Author Topic: Multidimensional arrays in Cypress Basic?  (Read 446 times)
0 Members and 2 Guests are viewing this topic.
farmertom
Active Member

Offline Offline

Posts: 6


View Profile
« on: November 12, 2011, 03:00:49 PM »

Hello all,

In reading the Cypress Basic (CB) reference manual I see the CB supports multidimensional  arrays. But I can not find the answer to my questions.
1) How do you Dim an empty two dimensional array?  Dim myarray ( , ) as integer    -does not work.
2) If you get a two dimensional array declared, how do you ReDim it?

Thanks for your time and advice
farmertom
Logged
Sargon
Active Member

Offline Offline

Posts: 99


View Profile
« Reply #1 on: November 17, 2011, 06:18:59 AM »

AFAIK you cannot initialize an empty multi-dimensional array. and you cannot ReDim it either as ReDim only accepts 1 parameter.

You could use a single array to do the same job though. You just have to manage the array yourself.

e.g.

Sub Main
   Dim MultiArray() As Integer
   Dim I As Integer, J As Integer
   Dim Dim1 As Integer, Dim2 As Integer
   
   Dim1 = 3
   Dim2 = 3
   
   Call ReDimArray(MultiArray(), Dim1, Dim2)

   For I = 0 To 3
      For J = 0 To 3
         Call SetArray(MultiArray, I, J, I + J)
         Print GetArray(MultiArray, I, J)
      Next J
   Next I
End Sub


Function ReDimArray(ByRef MultiArray() As Integer, dim1 As Integer, dim2 As Integer)
   ReDim MultiArray(dim1 * dim2)
End Function

Function SetArray(ByRef MultiArray() As Integer, dim1 As Integer, dim2 As Integer, value As Integer)
   MultiArray(dim1 * dim2) = value
End Function

Function GetArray(ByRef MultiArray() As Integer, dim1 As Integer, dim2 As Integer) As Integer
   GetArray = MultiArray(dim1 * dim2)
End Function
Logged
Sargon
Active Member

Offline Offline

Posts: 99


View Profile
« Reply #2 on: November 17, 2011, 06:34:20 AM »

Correction:

Dim DimSize1 As Integer, DimSize2 As Integer

Sub Main
   Dim MultiArray() As Integer
   Dim I As Integer, J As Integer
   
   DimSize1 = 3
   DimSize2 = 3
   
   Call ReDimArray(MultiArray(), DimSize1, DimSize2)

   'insert dummy data
   For I = 0 To 8
      MultiArray(I) = I
   Next I
   
   For I = 0 To 2
      For J = 0 To 2
'         Call SetArray(MultiArray, I, J, I + J)
         Print GetArray(MultiArray, I, J)
      Next J
   Next I
End Sub


Function ReDimArray(ByRef MultiArray() As Integer, loc1 As Integer, loc2 As Integer)
   ReDim MultiArray(loc1 * loc2)
End Function

Function SetArray(ByRef MultiArray() As Integer, loc1 As Integer, loc2 As Integer, value As Integer)
   MultiArray(loc1 + loc2 * DimSize1) = value
End Function

Function GetArray(ByRef MultiArray() As Integer, loc1 As Integer, loc2 As Integer) As Integer
   GetArray = MultiArray(loc1 + loc2 * DimSize2)
End Function

Logged
farmertom
Active Member

Offline Offline

Posts: 6


View Profile
« Reply #3 on: November 17, 2011, 08:00:08 AM »

Sargon,
Thanks for the reply. I did eventually go with a one dimensional array, but your idea of indexing the second dimension is a great idea. I will use that in future programing. Too bad CB is so limited.  Thanks for the time and effort to reply to my post.
regards
farmertom
Logged
WilliamThomas
Holding

Offline Offline

Posts: 1



View Profile
« Reply #4 on: December 19, 2011, 07:57:46 AM »

Arrays should be initialized some values, we cannot left blank, whether it is a multidimensional array or one dimensional array.
Logged

Sargon
Active Member

Offline Offline

Posts: 99


View Profile
« Reply #5 on: December 19, 2011, 07:10:57 PM »

The question pertained to resizing a multidimensional array. Initializing it wouldn't help accomplish this.
Logged
Pages: 1   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!