Two-Dimensional Arrays
Two-dimensional arrays variables store the contents of tables or matrices. Example: Dim arrTable(1 to 5, 1 to 5) As Integer first row last row first column last column
Using For Loops to Manipulate Arrays You can efficiently process a multidimensional array by using nested For loops. The outer loop of the For loop controls the rows, and the inner loop of the For loop controls the columns
Private Sub FillArray(A() As Double) Dim I As Integer, J As Integer For I = 1 To 5 For J = 1 To 5 A(I, J) = I * 10 + J Next J Next I End Sub
Private Sub DisplayArray(A() As Double) Dim I As Integer, J As Integer For I = 1 To 5 For J = 1 To 5 picOutput.Print A(I, J); Next J picOutput.Print Next I End Sub Private Sub Command1_Click() Dim MatrixA(1 To 5, 1 To 5) As Double Call FillArray(MatrixA()) Call DisplayArray(MatrixA()) End Sub
Reading Table Data from a Data File into a Two-Dimensional Array Chicago Los Angles New York Philadelphia Chicago Los Angles New York Philadelphia
Data File for the Table Data 0, 2054, 802, , 0, 2786, , 2786, 0, , 2706, 100, 0
Sub Procedure to Input Data from the Data File into a Two-dimensional Array Private Sub Form_Load() Dim arrTable(1 To 4, 1 To 4) As Single Dim row As Integer, col As Integer 'Fill two-dimensional array with intercity mileages 'Assume the data has been placed in the file "DISTANCE.TXT" '(First line of the file is 0, 2054, 802, 738) Open App.Path & "\DISTANCE.TXT" For Input As #1 For row = 1 To 4 For col = 1 To 4 Input #1, arrTable(row, col) Next col Next row Close #1 End Sub