Multiple Dimensions of Arrays

ATEasy allows multi-dimensional arrays. A two dimensional array is an array of arrays, a three dimensional array is an array of arrays of arrays, etc. The individual values are referred to by the array name followed by a series of integer expressions, separated by commas, and enclosed in square brackets. For example, af[i, j,k] refers to one value in a 3-dimensional array of floats.

Array Slices

The values are stored is row-major order, that is, the right-most subscript varies most rapidly. Thus, if an is a 3 by 2 array of shorts, then the individual values are stored in the order an[0,0], an[0,1], an[1,0], an[1,1] , an[2,0], an[2,1]. This storage layout facilitates the extraction of sub-arrays, or slices, from a multi-dimensional array. A sub-array or slice can be referred to by using fewer subscripts than the array has dimensions.

The right-most subscript can be omitted to refer to a 1-dimensional slice, the right-most two subscripts can be omitted to refer to a two dimensional slice, etc. Thus, if ad is a 3 by 4 by 5 array of doubles, it can be thought of as an array of 3 elements, each of which is an array of 4 elements, each of which is an array of 5 doubles. The first double value is ad[0,0,0] and the last is ad[2,3,4]. The first 1-dimensional slice is ad[0,0] and the last is ad[2,3]. The first 2-dimensional slice is ad[0] and the last is ad[2]. A reference to ad without a subscript is a reference to the whole array.

If a is a two dimensional array, then a[i,j] and a[i][j] both refer to the same element. However the first form is far more efficient as the run time uses the subscripts to calculate the linear offset of the desired element in the list of values. The second form creates a temporary array variable consisting of the i'th slice of the array and then subscripts that temporary array to get the j'th element.

Calculating array dimension size

Array dimension can be calculated dynamically using the sizeof operator as the following example demonstrates:

The following code sample assumes that 25 samples have been made of the voltages on 7 different pins and calculates the average voltage for each pin. The first subscript of the 2-dimensional array adPinReadings is the pin number and the second subscript is the sample number. The 7 slices of the 2-dimensional adPinReadings array are passed to the function DblAverage to calculate the average of the 25 readings for each pin.

adPinReadings: Double[7,25]

adPinAverages: Double[7]

i: Long

iCount: Long

 

iCount=sizeof(adPinReadings) div sizeof(adPinReadings[0])    ! 7

for i=0 to iCount-1

adPinAverages[n] = DblAverage(adPinReadings[n], 25)

next

See Also

Arrays (Definition), Arrays as Procedure Arguments, Arrays of Variants, Assignment of Arrays, Strings as Arrays