' Visual Basic 2008 9.0 .NET Examples - Introduction to Arrays - Arrays : Module Example1 Sub Main() Dim value1, value2, value3 As Integer Dim sum As Integer ' Console.WriteLine("Value 1 ?") value1 = Console.ReadLine() sum += value1 ' Console.WriteLine("Value 2 ?") value2 = Console.ReadLine() sum += value2 ' Console.WriteLine("Value 3 ?") value3 = Console.ReadLine() sum += value3 ' Console.Write(value1) ' Console.Write(" + ") Console.Write(value2) ' Console.Write(" + ") Console.Write(value3) ' Console.Write(" = ") Console.Write(sum) ' Console.ReadLine() End Sub End Module Module Example2 Sub Main() Dim values(2) As Integer Dim sum As Integer ' Console.WriteLine("Value 1 ?") values(0) = Console.ReadLine() sum += values(0) ' Console.WriteLine("Value 2 ?") values(1) = Console.ReadLine() sum += values(1) ' Console.WriteLine("Value 3 ?") values(2) = Console.ReadLine() sum += values(2) ' Console.Write(values(0)) ' Console.Write(" + ") Console.Write(values(1)) ' Console.Write(" + ") Console.Write(values(2)) ' Console.Write(" = ") Console.Write(sum) ' Console.ReadLine() End Sub End Module Module Example3 Sub Main() Dim values(2) As Integer Dim sum As Integer ' Dim index As Integer For index = 0 To 2 Console.WriteLine("Value " & index + 1 & " ?") values(index) = Console.ReadLine() sum += values(index) Next ' Console.Write(values(0)) For index = 1 To 2 Console.Write(" + ") Console.Write(values(index)) Next Console.Write(" = ") Console.Write(sum) ' Console.ReadLine() End Sub End Module Module Example4 Sub Main() Console.WriteLine("Count ?") Dim count As Integer = Console.ReadLine() ' Dim values(count) As Integer Dim sum As Integer ' Dim index As Integer For index = 0 To count - 1 Console.WriteLine("Value " & index + 1 & " ?") values(index) = Console.ReadLine() sum += values(index) Next ' Console.Write(values(0)) For index = 1 To count - 1 Console.Write(" + ") Console.Write(values(index)) Next Console.Write(" = ") Console.Write(sum) ' Console.ReadLine() End Sub End Module Module Example5 Sub Main() Dim values(4) As Integer Dim conditions(4) As Boolean ' Dim index As Integer For index = 0 To 4 Console.WriteLine(values(index)) ' (1) Console.WriteLine(conditions(index)) ' (2) Next ' 'values(5) = 5 ' runtime error, no element at index 5 ' Console.ReadLine() End Sub End Module Module Exercise1Solution Sub Main() Dim count As Integer = 5 Dim upperbound As Integer = count - 1 ' Dim values(upperbound) As Integer ' Dim index As Integer For index = 0 To upperbound Console.Write("Value " & (index + 1) & " : ") values(index) = Console.ReadLine() Next ' For index = upperbound To 0 Step -1 Console.WriteLine("Element " & (index + 1) & _ " ( at index " & (index) & " ) : " & _ values(index)) Next ' Console.ReadLine() End Sub End Module Module Exercise2Solution Sub Main() Console.Write("Count : ") Dim count As Integer = Console.ReadLine() Dim upperbound As Integer = count - 1 ' Dim values(upperbound) As Integer Dim index, highestValueIndex As Integer ' For index = 0 To upperbound Console.Write("Value " & (index + 1) & " : ") values(index) = Console.ReadLine() If values(highestValueIndex) < values(index) Then highestValueIndex = index End If Next ' Console.WriteLine("Highest Value : " & values(highestValueIndex) & _ " ( element " & (highestValueIndex + 1) & _ " at index " & highestValueIndex & " )") ' Console.ReadLine() End Sub End Module Module Exercise3Solution Sub Main() Dim values(9) As Integer ' Dim index As Integer For index = 0 To 9 values(index) = 10 - index Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values(index)) Next ' Console.ReadLine() End Sub End Module Module Exercise4Solution Sub Main() Dim count As Integer = 10 Dim upperbound As Integer = count - 1 Dim values(upperbound) As Integer Dim index As Integer ' For index = 0 To upperbound values(index) = index + 1 Next ' For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values(index)) Next Console.WriteLine() ' Dim backup As Integer For index = 0 To (count \ 2) - 1 backup = values(index) values(index) = values(upperbound - index) values(upperbound - index) = backup Next ' For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values(index)) Next ' Console.ReadLine() End Sub End Module Module Exercise5Solution Sub Main() Dim count As Integer = 10 Dim upperbound As Integer = count - 1 Dim values(upperbound) As Integer Dim index As Integer ' For index = 0 To upperbound values(index) = index + 1 Next ' For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values(index)) Next Console.WriteLine() ' Dim backup As Integer = values(0) For index = 0 To (upperbound - 1) values(index) = values(index + 1) Next values(upperbound) = backup ' For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values(index)) Next ' Console.ReadLine() End Sub End Module Module Exercise6Solution Sub Main() Dim count As Integer = 10 Dim upperbound As Integer = count - 1 Dim values1(upperbound) As Integer Dim values2(upperbound) As Integer Dim values3(upperbound) As Integer Dim index As Integer ' For index = 0 To upperbound values1(index) = index + 1 Next ' Console.WriteLine("Array 1 :") For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values1(index)) Next Console.WriteLine() ' For index = 0 To upperbound values2(index) = count - index Next ' Console.WriteLine("Array 2 :") For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values2(index)) Next Console.WriteLine() ' For index = 0 To upperbound If values1(index) >= values2(index) Then values3(index) = values1(index) Else values3(index) = values2(index) End If Next ' Console.WriteLine("Array 3 :") For index = 0 To upperbound Console.WriteLine("element " & (index + 1) & _ " ( index " & index & " ) : " & _ values3(index)) Next Console.WriteLine() ' Console.ReadLine() End Sub End Module Module Exercise7Solution Sub Main() Console.Write("Count : ") Dim count As Integer = Console.ReadLine() ' If count <= 0 Then Console.WriteLine("Error : Minimum count 1.") Else Dim upperbound As Integer = count - 1 Dim fibonacci(upperbound) As Integer Dim index As Integer ' fibonacci(0) = 1 If count > 1 Then fibonacci(1) = 1 For index = 2 To upperbound fibonacci(index) = fibonacci(index - 1) + fibonacci(index - 2) Next End If ' For index = 0 To upperbound Console.Write(fibonacci(index) & " ") Next End If ' Console.ReadLine() End Sub End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.