Hello Guest it is February 26, 2021, 11:41:13 PM

Author Topic: Multidimensional arrays in Cypress Basic?  (Read 5669 times)

0 Members and 1 Guest are viewing this topic.

Multidimensional arrays in Cypress Basic?
« 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
Re: Multidimensional arrays in Cypress Basic?
« 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
Re: Multidimensional arrays in Cypress Basic?
« 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

Re: Multidimensional arrays in Cypress Basic?
« 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
Re: Multidimensional arrays in Cypress Basic?
« 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.
Re: Multidimensional arrays in Cypress Basic?
« 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.