Multi-Dimensional Arrays 


 

Two-Dimensional Arrays

One-dimensional arrays resemble lists.  Two-dimensional arrays resemble a table or matrix with rows and columns.

══════════════════

Declaration

Multi-dimensional arrays are declared like one-dimensional arrays.

Dim table (5, 3) As Integer

══════════════════

examples:

    Dim matrix (4, 2) As Integer

               

row 

column

Array matrix is a 2-dimensional array with 5 rows and 3 columns--or 15 elements.

══════════════════

Elements in 2-D arrays are referred to by the array variable name and two indexes--one for each dimension.

ex.    matrix (3,2)
       
matrix (3, J)
       
matrix (M, J)
       
matrix (N+1, J)

Using the declaration above, along with the following declarations:

   Dim rowCtr As Integer, colCtr As Integer

the program segment below will create a portion of a multiplication table:

    For rowCtr = 0 to matrix.GetUpperBound(0) 
        For colCtr = 0 to matrix.GetUpperBound(1) 
            matrix(rowCtr , colCtr ) = rowCtr * colCtr
        Next colCtr
    Next rowCtr

           

Notice that one row was processed at a time.  Since the loop that contained rowCtr as a loop control variable was the outermost loop, the inner loop was processed completely for each increment of rowCtr, i.e., rowCtr =1: colCtr = 1, 2, 3; rowCtr = 2: colCtr = 1, 2, 3; etc.  In nested loops the innermost loop runs faster.

Note:  For loops are ideal for processing an entire array.  However, if you want to stop processing when a certain condition is met then the For loop should not be used.  The For loop is a count-controlled loop, and should include no additional terminating conditions.  In such cases a While loop is preferable.

══════════════════

The following code demonstrates the necessity of the While loop.  It creates a partial multiplication table from a 6 x 6 array (dimensioned as prod(5, 5)) with the added restriction that the results can be no more than single digits:

   For rowCtr = 0 to prod.GetUpperBound(0)
        colCtr = 0
        Single_Digit = True
        While (colCtr <= prod.GetUpperBound(1)) And Single_Digit
                product = rowCtr * colCtr
                If (product < 10) Then
                    prod(rowCtr, colCtr) = product
                Else
                    Single_Digit = False
                EndIf
                colCtr += 1
        End While
    Next Row

══════════════════

GetUpperBound -- The complete syntax for GetUpperBound is

    arrayname.GetUpperBound(dimension)

The dimension argument is a whole number indicating which dimension's upper bound is returned.  Use 0 for the first dimension, 1 for the second, and so on.  So 0 corresponds to the row in a two-dimensional array, and 1 corresponds to the column.

Dim testArray (100, 3)

Statement

Return value

testArray.getUpperBound(0)

100

testArray.getUpperBound(1)

3


Array Processing 

  1. Access each element in a systematic way

    1. By rows

            For rowCtr = 0 to testArray.GetUpperBound(0)
                For colCtr = 0 to testArray.GetUpperBound(1)
                    ' Process testArray(rowCtr, colCtr)
                Next colCtr
            Next rowCtr

     

    1. By columns

            For colCtr = 0 to testArray.GetUpperBound(1)
                For rowCtr = 0 to testArray.GetUpperBound(0)
                    ' Process testArray(rowCtr, colCtr)
                Next rowCtr
            Next colCtr

  1. Access a random element

        testArray(rowCtr, colCtr)

  1. Process a single row (rowCtr is held constant while colCtr changes.)

        rowCtr = <some value>
        For colCtr = 0 to testArray.GetUpperBound(1)
               ' Process testArray(rowCtr, colCtr)
        Next colCtr

 

  1. Process a single column (colCtr is held constant while rowCtr changes.)

        colCtr = <some value>
        For
rowCtr = 0 to testArray.GetUpperBound(0)
               ' Process testArray(rowCtr, colCtr)
        Next
rowCtr

  1. Process a diagonal -- Limit = smallest upper bound

limit = IIf (testArray.GetUpperBound(0) < testArray.GetUpperBound(1), _ 
              testArray.GetUpperBound(0), testArray.GetUpperBound(1))

OR

If (testArray.GetUpperBound(0) < testArray.GetUpperBound(1)) Then
   limit = testArray.GetUpperBound(0)
Else
   limit = testArray.GetUpperBound(1)
EndIf

 

  1. Left to right  

        For diag = 0 to limit
            ' Process testArray(diag, diag)
        Next diag

  1. Right to left

        For diag = 0 to limit
            ' Process testArray(diag, limit - diag)
        Next diag

When using procedures, passing the row and column limits as parameters allows partial processing of arrays.


Multi-Dimensional Arrays

Visual Basic allows arrays with more than two dimensions. The number of dimensions of an array is the same as the number of indexes needed to reference a particular cell. 

This example stores sales data for various items in multiple stores for each of the months, or three dimensional data.

        Dim sales (49, 9, 11) As Single

The total number of elements is the product of the number of elements in each dimension of the array, in this case 50 x 10 x 12, or 6000 elements.

One way to keep track of where you are in a multi-dimensional array is to let the first subscript (left-hand one) be referenced by the outermost loop, the second subscript by the next to the outermost loop, etc., until the last subscript is referenced by the innermost loop.

   Dim cube (2, 1, 1) As Integer
   Dim heightCtr As Integer, lengthCtr As Integer, widthCtr As Integer

    For heightCtr = 0 To cube.GetUpperBound(0)
        For lengthCtr = 0 To cube.GetUpperBound(1)
                For widthCtr = 0 To cube.GetUpperBound(2)
                    cube(heightCtr, lengthCtr, widthCtr) = 0
               Next widthCtr
        Next lengthCtr
    Next heightCtr

Note that the widthCtr, or innermost loop, varies most rapidly, then the lengthCtr, then the heightCtr.  Always declare an array large enough to hold the maximum number of entries, i.e., make it larger than the anticipated need (but not overly large). 

══════════════════

To declare a dynamic two-dimensional array, declare the array with a comma instead of dimensions, and specify the size in the ReDim statement.

Dim testArray ( , ) as Integer

ReDim testArray (10, 15)


Passing Multi-Dimensional Arrays to Procedures

To pass an entire multi-dimensional array to a procedure, specify the array name as the argument.

For example, if array testArray is declared as 

            Dim testArray(10,15) As Integer

the call 

Call modify2DArray(testArray

passes array testArray to procedure modify2DArray.

 


Receiving Multi-Dimensional Arrays in Procedures

For a procedure to receive an entire array through a call, the parameter list must specify that an array will be received by including a comma in parenthesis (1 comma for a 2D array, 2 commas for a 3D array, etc.).

For example, the procedure header for modify2DArray might be written as

   Private Sub modify2DArray(ByVal receivedArray( , ) As Integer)

indicating that modify2DArray expects to receive an Integer array in parameter receivedArray.

The size of the array is not specified between the array parentheses, although the number of dimensions is implied.