|
| When no upperbound is used in the declaration of an array variable, the array can be initialized. This can be done by adding an initialization clause to the declaration, containing initial element values, separated by commas, and surrounded by braces ( {...} ).
The arrayinitializer is a expression that will create the array instance ( with the defined values ), and will evaluate to the reference of that array instance. That reference can then be assigned to the array variable. |
| Module Example
Sub Main()
Dim indexFirstDimension As Integer
Dim indexSecondDimension As Integer
Dim indexThirdDimension As Integer
Dim row() As Integer = {1, 2, 3, 4, 5, 6}
For indexFirstDimension = 0 To 5
Console.Write(row(indexFirstDimension) & " ")
Next
Console.WriteLine()
Dim matrix(,) As Integer = {{1, 2, 3, 4, 5, 6}, _
{7, 8, 9, 10, 11, 12}}
For indexFirstDimension = 0 To 1
For indexSecondDimension = 0 To 5
Console.Write(matrix(indexFirstDimension, _
indexSecondDimension) & " ")
Next
Next
Console.WriteLine()
Dim cube(,,) As Integer = {{{1, 2, 3}, _
{4, 5, 6}}, _
{{7, 8, 9}, _
{10, 11, 12}}, _
{{13, 14, 15}, _
{16, 17, 18}}, _
{{19, 20, 21}, _
{22, 23, 24}}}
For indexFirstDimension = 0 To 3
For indexSecondDimension = 0 To 1
For indexThirdDimension = 0 To 2
Console.Write(cube(indexFirstDimension, _
indexSecondDimension, _
indexThirdDimension) & " ")
Next
Next
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
| 1st index : 0 1 2 3 4 5
value : 1 2 3 4 5 6 |
| 1st index : 0 0 0 0 0 0 1 1 1 1 1 1
2nd index : 0 1 2 3 4 5 0 1 2 3 4 5
value : 1 2 3 4 5 6 7 8 9 10 11 12 |
| 1st index : 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3
2nd index : 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
3rd index : 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
value : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Exercise
| Task :
Create a program that can convert integral values ( between 0 and 4000 ) to a Roman numeral.
I = 1, IV = 4, V = 5, IX = 9, X = 10, XL = 40, L = 50, XC = 90, C = 100, CD = 400, D = 500, CM = 900, M = 1000
Use arrays to store these symbols and conversion values. |
| Output : Value ?
-1
Value ?
4000
Value ?
1
I
Value ?
3999
MMMCMXCIX
Value ?
2789
MMDCCLXXXIX
Value ? |
| Module ExerciseSolution
Sub Main()
Dim symbols() As String = _
{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
Dim units() As Integer = _
{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
Dim value, index As Integer
Do
Do
Console.WriteLine("Value ?")
value = Console.ReadLine()
Loop Until value < 4000 AndAlso value >= 0
For index = 0 To 12
Do While value >= units(index)
Console.Write(symbols(index))
value -= units(index)
Loop
Next
Console.WriteLine()
Loop
End Sub
End Module Download Broncode |
|
|
|