146 lines
3.8 KiB
Text
146 lines
3.8 KiB
Text
|
|
//
|
||
|
|
// Multi-Dimensional Array
|
||
|
|
// Using FutureBasic 7.0.34, August 2025 R.W
|
||
|
|
|
||
|
|
window 1
|
||
|
|
|
||
|
|
Int my2DArray(2, 3) // traditional 2 x 3
|
||
|
|
|
||
|
|
// myArray: 0,0 - 0,1 - 0,2 - 0,3
|
||
|
|
// 1,0 - 1,1 - 1,2 - 1,3
|
||
|
|
// 2,0 - 2,1 - 2,2 - 2,3
|
||
|
|
|
||
|
|
// This is like a theater seating chart
|
||
|
|
// of three rows with four seats in each row
|
||
|
|
|
||
|
|
// to index or point to each position, you
|
||
|
|
// need two numbers to address the row and seat (column)
|
||
|
|
|
||
|
|
Int rowIndex, colIndex
|
||
|
|
|
||
|
|
for rowIndex = 0 to 2 // three rows
|
||
|
|
for colIndex = 0 to 3 // of four seats
|
||
|
|
my2DArray(rowIndex, colIndex) = rnd(6) //assign random number 1-6
|
||
|
|
print my2DArray(rowIndex, colIndex);" "; //see the contents
|
||
|
|
next colIndex
|
||
|
|
print
|
||
|
|
next rowIndex
|
||
|
|
|
||
|
|
// The elements of my2DArray(2,3) are stored in row-major-order
|
||
|
|
// in contiguous locations in memory:
|
||
|
|
|
||
|
|
// my2DArray (0,0)
|
||
|
|
// my2DArray (0,1)
|
||
|
|
// my2DArray (0,2)
|
||
|
|
// my2DArray (0,3)
|
||
|
|
// my2DArray (1,0)
|
||
|
|
// my2DArray (1,1)
|
||
|
|
// my2DArray (1,2)
|
||
|
|
// my2DArray (1,3)
|
||
|
|
// my2DArray (2,0)
|
||
|
|
// my2DArray (2,1)
|
||
|
|
// my2DArray (2,2)
|
||
|
|
// my2DArray (2,2)
|
||
|
|
|
||
|
|
// Here is a 4 dimansional array
|
||
|
|
|
||
|
|
Double my4DArray(2,3,3,3) // A traditional 4 dimensional array
|
||
|
|
my4DArray(0,0,0,1) = 4 // asigning value into the array
|
||
|
|
|
||
|
|
|
||
|
|
print@
|
||
|
|
|
||
|
|
// Non traditional MDA array, does not need to be declared.
|
||
|
|
// MDA arrays are mutable, single or multi-dimensional, always
|
||
|
|
// global in scope and are zero-based.
|
||
|
|
//
|
||
|
|
// MDA arrays are identified by a tag (if omitted mda 0 is used).
|
||
|
|
// To help identify the MDA arrays (if you're using more than one)
|
||
|
|
// you can assign constants to the tag, for example:
|
||
|
|
//
|
||
|
|
// _cardDeck = 1
|
||
|
|
// _Lights = 2
|
||
|
|
//
|
||
|
|
// You can then identify the MDA array as such
|
||
|
|
//
|
||
|
|
// mda _cardDeck(0) = @"AC"
|
||
|
|
// mda _cardDeck(1) = @"1C"
|
||
|
|
//
|
||
|
|
// mda _Lights(0) = LEDseq1
|
||
|
|
// etc.
|
||
|
|
//
|
||
|
|
// Which will help you identify in your code which array you
|
||
|
|
// are dealing with (instead of remembering numerical values).
|
||
|
|
//
|
||
|
|
// In the example below, the mda has a tag of 1 to identify it.
|
||
|
|
// While MDA stands for Multi Dimensional Array, it can also
|
||
|
|
// be used for a single dimensional use.
|
||
|
|
|
||
|
|
// What makes MDA versatile is the fact that you can assign
|
||
|
|
// anything.
|
||
|
|
|
||
|
|
// In a traditional array where you defined the variable
|
||
|
|
// type, you are committed to that type. For example, if
|
||
|
|
// you declared Int my2DArray(2, 3), you are committed to
|
||
|
|
// assign only integer values into the array. With an MDA
|
||
|
|
// array you can place whatever value, as this example
|
||
|
|
// below
|
||
|
|
|
||
|
|
mda 1(0,0) = 1.5 // it can have a number
|
||
|
|
mda 1(1,0) = @"Charlie" // it can have a string
|
||
|
|
mda 1(2,0) = PI // it can have constant
|
||
|
|
|
||
|
|
print@ mda 1(0,0), mda 1(1,0), mda 1 (2,0)
|
||
|
|
CGRect r // declare r to be a rectangle record
|
||
|
|
r.origin.x = 10 // x = 10
|
||
|
|
r.origin.y = 20 // y = 20
|
||
|
|
r.size.width = 300 // width = 300
|
||
|
|
r.size.height = 150 // height = 150
|
||
|
|
|
||
|
|
// Stuff this record/structure into an mda array element
|
||
|
|
|
||
|
|
mda 1(0,1) = r
|
||
|
|
|
||
|
|
print @"Contents of mda 1(0,1) = "; mda 1(0,1) // see if it's there
|
||
|
|
print@
|
||
|
|
|
||
|
|
// you can also transfer the record in its entirety
|
||
|
|
|
||
|
|
CGRect newRect
|
||
|
|
newRect = mda 1(0,1)
|
||
|
|
|
||
|
|
// see if it transfers
|
||
|
|
print @"Contents of newRect = mda 1(0,1)"
|
||
|
|
print @" x = ";newRect.origin.x
|
||
|
|
print @" y = ";newRect.origin.y
|
||
|
|
print @"width = ";newRect.size.width
|
||
|
|
print @"height= ";newRect.size.height
|
||
|
|
|
||
|
|
// MDA type array also has built in helper which doesn't exist
|
||
|
|
// in traditional arrays such as sort ability.
|
||
|
|
|
||
|
|
mda 2(0) = @"Zulu"
|
||
|
|
mda 2(1) = @"Charlie"
|
||
|
|
mda 2(2) = @"Alpha"
|
||
|
|
|
||
|
|
print@
|
||
|
|
print @"Contents of mda 2"
|
||
|
|
Int x
|
||
|
|
for x = 0 to 2:print@ x;@" = "; mda 2(x): next x
|
||
|
|
print @
|
||
|
|
|
||
|
|
mda_sort 2 // sort the array
|
||
|
|
|
||
|
|
print @"Sorted:"
|
||
|
|
for x = 0 to 2:print@ x;@" = "; mda 2(x): next x
|
||
|
|
|
||
|
|
// Another helper example
|
||
|
|
|
||
|
|
print@
|
||
|
|
print @"mda 2 array has ";mda_count 2;@" elements"
|
||
|
|
//
|
||
|
|
// For more info see documentation of mda arrays at
|
||
|
|
// https://www.brilorsoftware.com/fb/documentation/FBHelp/pgs/mda.html
|
||
|
|
|
||
|
|
handleEvents
|