Multidimensional Arrays
| So far all arrays we've used were one-dimensional.
Let's have a look at multidimensional arrays.
First the declaration : |
| Dim <identifier>(<first-upperbound>, <second-upperbound> [, ...]) [<type-specifier>]
|
| Between the parentheses all upperbounds ( of all dimensions ) are defined. All upperbounds are separated by a comma ( , ).
Following example declares a two-dimensional array called matrix. Usually we refer to the first dimension as being the rows, and the second dimension as being the columns. Here we have 2 rows and 3 columns. |
| Module Example1
Sub Main()
Dim rowCount As Integer = 2
Dim columnCount As Integer = 3
Dim upperboundFirstDimension As Integer = rowCount - 1
Dim upperboundSecondDimension As Integer = columnCount - 1
Dim matrix(upperboundFirstDimension, upperboundSecondDimension) As Integer
matrix(0, 0) = 10
matrix(1, 0) = 20
matrix(1, 2) = 30
Console.WriteLine("first row, first column : " & matrix(0, 0))
Console.WriteLine("second row, first column : " & matrix(1, 0))
Console.WriteLine("second row, second column : " & matrix(1, 1))
Console.WriteLine("third row, third column : " & matrix(1, 2))
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : first row, first column : 10
second row, first column : 20
second row, second column : 0
third row, third column : 30 |
| A possible representation of matrix ( with 2 rows and 3 columns ) could be : |
| column-index 0 1 2
row-index
0 10 0 0
1 20 0 30 |
| Although we usually speak about the first dimension of being the rows and the second dimension as being the columns in a two-dimension array, this is a fictional representation. In memory values can only be stored in one dimension, there is no possibility to address something in memory using multiple dimensions.
So in memory the values are rather stored like this : |
| row-index : 0 0 0 1 1 1
column-index : 0 1 2 0 1 2
values : 10 0 0 20 0 30 |
| The first ( fictional ) representation is easier to image, and corresponds better with how we address the elements.
Multidimensional arrays are only used when multidimensional access to elements is preferred. An obvious example would be a chessboard, where a rank ( 1 - 8 ) and a file ( a - h ) are used to address a square or piece.
When two For ... Nexts are nested, and one iterates over the index range of the first dimension and the other iterates over the index range of the second dimension, it is very easy to perform an operation on all elements of a two-dimensional array.
Suppose we need to fill a two-dimensional array with 10 rows and 10 columns with values 101 to 200. |
| Module Example2
Sub Main()
Dim rowCount As Integer = 10
Dim columnCount As Integer = 10
Dim upperboundFirstDimension As Integer = rowCount - 1
Dim upperboundSecondDimension As Integer = columnCount - 1
Dim values(upperboundFirstDimension, _
upperboundSecondDimension) As Integer
Dim startValue As Integer = 101
Dim rowIndex, columnIndex As Integer
For rowIndex = 0 To upperboundFirstDimension
For columnIndex = 0 To upperboundSecondDimension
values(rowIndex, columnIndex) = startValue
startValue += 1
Console.Write(values(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 |
| A more dynamical solution could base the value ( to assign to a certain element ) on the row and column index of that element.
This is a more dynamical solution because the value we assign is not depend on the iteration. |
| Module Example3
Sub Main()
Dim rowCount As Integer = 10
Dim columnCount As Integer = 10
Dim upperboundFirstDimension As Integer = rowCount - 1
Dim upperboundSecondDimension As Integer = columnCount - 1
Dim values(upperboundFirstDimension, upperboundSecondDimension) As Integer
Dim baseValue As Integer = 101
Dim rowIndex, columnIndex As Integer
For rowIndex = 0 To upperboundFirstDimension
For columnIndex = 0 To upperboundSecondDimension
values(rowIndex, columnIndex) = _
baseValue + (rowIndex * 10) + columnIndex
Console.Write(values(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 |
| An element ( in array values ) on a specific row ( at rowIndex ) on a specific column ( at columnIndex ) is assigned a value. |
| values(rowIndex, columnIndex) = value
|
| An overview of the assigned values : |
| rowIndex columnIndex value
0 0 101
0 1 102
0 2 103
... ... ...
0 9 110
1 0 111
1 1 112
1 2 113
... ... ...
1 9 120
... ... ...
9 9 200 |
| The value ( to assign ) can be base on the baseValue ( 101 ), rowIndex and columnIndex. |
| value = baseValue + (rowIndex * 10) + columnIndex
|
| Three-, four-, five-, six-, seven-, ... dimensional arrays can be created, but are rarely used. |
Up
Exercises
| Task :
Create a program that fills a two-dimensional array with ascending values. The number of rows, the number of columns and the starting value is determined by the user.
Print out all elements of this array.
Now shift all element one position to the right, and print out the array. |
| Output : Row Count ?
<i>5</i>
Column Count ?
<i>9</i>
Start Value ?
<i>11</i>
11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37
38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55
55 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 |
| Module Exercise1Solution
Sub Main()
Console.WriteLine("Row Count ?")
Dim rowCount As Integer = Console.ReadLine()
Console.WriteLine("Column Count ?")
Dim columnCount As Integer = Console.ReadLine()
Dim rowUpperbound As Integer = rowCount - 1
Dim columnUpperbound As Integer = columnCount - 1
Dim values(rowUpperbound, columnUpperbound) As Integer
Console.WriteLine("Start Value ?")
Dim startValue As Integer = Console.ReadLine()
Console.WriteLine()
Dim rowIndex, columnIndex, value As Integer
value = startValue
For rowIndex = 0 To rowUpperbound
For columnIndex = 0 To columnUpperbound
values(rowIndex, columnIndex) = value
value += 1
Console.Write(values(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.WriteLine()
Dim backup As Integer
backup = values(rowUpperbound, columnUpperbound)
For rowIndex = rowUpperbound To 1 Step -1
For columnIndex = columnUpperbound To 1 Step -1
values(rowIndex, columnIndex) = values(rowIndex, columnIndex - 1)
Next
values(rowIndex, 0) = values(rowIndex - 1, columnUpperbound)
Next
For columnIndex = columnUpperbound To 1 Step -1
values(0, columnIndex) = values(0, columnIndex - 1)
Next
values(0, 0) = backup
For rowIndex = 0 To rowUpperbound
For columnIndex = 0 To columnUpperbound
Console.Write(values(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.WriteLine()
Console.ReadLine()
End Sub
End Module Download Broncode |
| Create a program that fills a two-dimensional array with ascending values. The number of rows, the number of columns and the starting value is determined by the user.
Print out all elements of this array.
Transpose the elements of the array, and print out all elements. |
| Output : Row Count ?
<i>8</i>
Column Count ?
<i>3</i>
Start Value ?
<i>11</i>
11 12 13
14 15 16
17 18 19
20 21 22
23 24 25
26 27 28
29 30 31
32 33 34
11 14 17 20 23 26 29 32
12 15 18 21 24 27 30 33
13 16 19 22 25 28 31 34 |
| Module Exercise2Solution
Sub Main()
Console.WriteLine("Row Count ?")
Dim rowCount As Integer = Console.ReadLine()
Console.WriteLine("Column Count ?")
Dim columnCount As Integer = Console.ReadLine()
Dim upperboundFirstDimension As Integer = rowCount - 1
Dim upperboundSecondDimension As Integer = columnCount - 1
Dim matrix(upperboundFirstDimension, _
upperboundSecondDimension) As Integer
Console.WriteLine("Start Value ?")
Dim startValue As Integer = Console.ReadLine()
Console.WriteLine()
Dim rowIndex, columnIndex, value As Integer
value = startValue
For rowIndex = 0 To upperboundFirstDimension
For columnIndex = 0 To upperboundSecondDimension
matrix(rowIndex, columnIndex) = value
value += 1
Next
Next
For rowIndex = 0 To upperboundFirstDimension
For columnIndex = 0 To upperboundSecondDimension
Console.Write(matrix(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.WriteLine()
Dim backup As Integer = upperboundFirstDimension
upperboundFirstDimension = upperboundSecondDimension
upperboundSecondDimension = backup
Dim transpone(upperboundFirstDimension, _
upperboundSecondDimension) As Integer
For rowIndex = 0 To upperboundFirstDimension
For columnIndex = 0 To upperboundSecondDimension
transpone(rowIndex, columnIndex) = _
matrix(columnIndex, rowIndex)
Console.Write(transpone(rowIndex, columnIndex) & " ")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|