Visual Basic 2008 9.0 .NET Examples and Ebook

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp

Introduction to Arrays

Vorig Onderwerp

Constants

|

Multidimensional Arrays

Volgend Onderwerp
Exercises

Exercises



Suppose we need a program that gives us the sum of 3 entered values. The sum together with the entered values has to be brought to the console.

Something like this :


Output :

 Value  1 ?
 <i>10</i>
 Value  2 ?
 <i>20</i>
 Value  3 ?
 <i>30</i>
 10 + 20 + 30 = 60

Following example is a possible solution.


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
Download Broncode

An alternative solution could use an array.

An array is a collection of elements of the same type. For instance a collection of Integer elements.


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
Download Broncode

Output :

 Value  1 ?
 <i>10</i>
 Value  2 ?
 <i>20</i>
 Value  2 ?
 <i>30</i>
 10 + 20 + 30 = 60

Every element of an array has an index. This index can be used to address the element. The index of the first element ( also called "lowerbound" ) is always 0, the index of the second element is 1, the index of the third element is 2, ..., the index of the last element ( also called "upperbound" ) is equal to the number of elements minus 1.
For instance an array of 10 elements, has upperbound 9.

To address an element of the array, both the identifier of the array variable and the index of the element are needed. For instance values(2) is a Integer expression resulting in the value of the third element.
All Integer expressions can be used to express the index of the element, for instance values(index) with index being an Integer variable containing 2, would also evaluate to the value of the third element.
values(index + 1) would then evaluate to the value of the fourth element.

Before an array is used, it is usually declared :


 Dim <identifier>(<upperbound>) <type-specifier>

The element of an array are stored together in memory.

An Integer array with 3 elements, will roughly consume 96 bits ( equal amount as 3 separate Integer variables ).

An advantage of using an array is that one can address the elements of this array in a dynamical fashion. The same expression ( for instance values(index) ) can be used several times at runtime, to address different elements. If index is 0, values(index) addresses the first element, if index is 1, values(index) addresses the second element, ... .
This cannot be done ( or is very hard to do ) when separate variables are used.


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
Download Broncode

Output :

 Value  1 ?
 <i>10</i>
 Value  2 ?
 <i>20</i>
 Value  2 ?
 <i>30</i>
 10 + 20 + 30 = 60

A For ... Next is ideal for performing an operation on all ( or some ) elements of the array.
A countervariable ( starting from the lowerbound, stepping 1 and ending at the upperbound ) can be used to dynamically and iteratively express all indexes of all elements.


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
Download Broncode

Output :

 Value  1 ?
 <i>10</i>
 Value  2 ?
 <i>20</i>
 Value  2 ?
 <i>30</i>
 10 + 20 + 30 = 60

Another advantage of arrays is that the size of the array ( the number of elements ) can be changed at runtime ( see next topic ).

Following examples illustrates how :
- all elements of an array contain the default value of the element type of that array, all elements of values contain 0 after declaration of values, all elements of conditions contain False after declaration of conditions
- a runtime error ( IndexOutOfRangeException ) will be produces when we address an element on a non-existing index


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
Download Broncode

Output :

 0
 False
 0
 False
 0
 False
 0
 False
 0
 False

Exercises


Task :

Make a program that fills an array with 5 elements with values entered by the user.

Print out all element in reverse order.


Output :

 Value 1 : <i>6</i>
 Value 2 : <i>2</i>
 Value 3 : <i>5</i>
 Value 4 : <i>4</i>
 Value 5 : <i>9</i>
 Element 5 ( at index 4 ) : 9
 Element 4 ( at index 3 ) : 4
 Element 3 ( at index 2 ) : 5
 Element 2 ( at index 1 ) : 2
 Element 1 ( at index 0 ) : 6

Solution :


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
Download Broncode

Task :


Make a program that first asks the user how many values to enter. The program will then fill an array with all entered values. When all values are entered the program prints out the highest entered value and its position ( and index ) in the array.


Output :

 Count : <i>3</i>
 Value  1 : <i>-8</i>
 Value  2 : <i>-2</i>
 Value  3 : <i>-3</i>
 Highest Value  : -2 ( element 2 at index 1 )

Solution :


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
Download Broncode

Task :


Make a program that fills an array with 10 elements with values 10 to 1 :


 index     0   1   2   3   4   5   6   7   8   9
           10  9   8   7   6   5   4   3   2   1

Only 2 variables may be used in this exercise.

Print out all elements.


Output :

 element 1 ( index 0 ) : 10
 element 2 ( index 1 ) : 9
 element 3 ( index 2 ) : 8
 element 4 ( index 3 ) : 7
 element 5 ( index 4 ) : 6
 element 6 ( index 5 ) : 5
 element 7 ( index 6 ) : 4
 element 8 ( index 7 ) : 3
 element 9 ( index 8 ) : 2
 element 10 ( index 9 ) : 1

Solution :


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
Download Broncode

Task :


Make a program that fills an array with 10 elements with values 1 to 10 :


 index     0   1   2   3   4   5   6   7   8   9
           1   2   3   4   5   6   7   8   9   10

Print out all elements.

Replace all elements until the are in reverse order :


 index     0   1   2   3   4   5   6   7   8   9
           10  9   8   7   6   5   4   3   2   1

Print out all elements.


Output :

 element 1 ( index 0 ) : 1
 element 2 ( index 1 ) : 2
 element 3 ( index 2 ) : 3
 element 4 ( index 3 ) : 4
 element 5 ( index 4 ) : 5
 element 6 ( index 5 ) : 6
 element 7 ( index 6 ) : 7
 element 8 ( index 7 ) : 8
 element 9 ( index 8 ) : 9
 element 10 ( index 9 ) : 10

 element 1 ( index 0 ) : 10
 element 2 ( index 1 ) : 9
 element 3 ( index 2 ) : 8
 element 4 ( index 3 ) : 7
 element 5 ( index 4 ) : 6
 element 6 ( index 5 ) : 5
 element 7 ( index 6 ) : 4
 element 8 ( index 7 ) : 3
 element 9 ( index 8 ) : 2
 element 10 ( index 9 ) : 1

Solution :


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
Download Broncode

Task :


Make a program that fills an array with 10 elements with values 1 to 10 :


 index     0   1   2   3   4   5   6   7   8   9
 value     1   2   3   4   5   6   7   8   9   10

Print out all elements.

Shift all elements one position to the left :


 index     0   1   2   3   4   5   6   7   8   9
 value     2   3   4   5   6   7   8   9   10  1

Print out all elements.


Output :

 element 1 ( index 0 ) : 1
 element 2 ( index 1 ) : 2
 element 3 ( index 2 ) : 3
 element 4 ( index 3 ) : 4
 element 5 ( index 4 ) : 5
 element 6 ( index 5 ) : 6
 element 7 ( index 6 ) : 7
 element 8 ( index 7 ) : 8
 element 9 ( index 8 ) : 9
 element 10 ( index 9 ) : 10

 element 1 ( index 0 ) : 2
 element 2 ( index 1 ) : 3
 element 3 ( index 2 ) : 4
 element 4 ( index 3 ) : 5
 element 5 ( index 4 ) : 6
 element 6 ( index 5 ) : 7
 element 7 ( index 6 ) : 8
 element 8 ( index 7 ) : 9
 element 9 ( index 8 ) : 10
 element 10 ( index 9 ) : 1

Solution :


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
Download Broncode

Task :


Make a program that fills an array with 10 elements with values 1 to 10 :


 array 1 :
 index     0   1   2   3   4   5   6   7   8   9
 value     1   2   3   4   5   6   7   8   9   10

Print out all elements.

Create a second array filled with values 10 to 1 :


 array 2 :
 index     0   1   2   3   4   5   6   7   8   9
 value     10  9   8   7   6   5   4   3   2   1

Print out all elements of this second array.

Create a third array and fill it with the highest of the two values of the elements of the first and second arrays at corresponding positions.


 array 3 :
 index     0   1   2   3   4   5   6   7   8   9
 value     10  9   8   7   6   6   7   8   9   10

Print out all elements of this third array.


Output :

 Array 1 :
 element 1 ( index 0 ) : 1
 element 2 ( index 1 ) : 2
 element 3 ( index 2 ) : 3
 element 4 ( index 3 ) : 4
 element 5 ( index 4 ) : 5
 element 6 ( index 5 ) : 6
 element 7 ( index 6 ) : 7
 element 8 ( index 7 ) : 8
 element 9 ( index 8 ) : 9
 element 10 ( index 9 ) : 10

 Array 2 :
 element 1 ( index 0 ) : 10
 element 2 ( index 1 ) : 9
 element 3 ( index 2 ) : 8
 element 4 ( index 3 ) : 7
 element 5 ( index 4 ) : 6
 element 6 ( index 5 ) : 5
 element 7 ( index 6 ) : 4
 element 8 ( index 7 ) : 3
 element 9 ( index 8 ) : 2
 element 10 ( index 9 ) : 1

 Array 3 :
 element 1 ( index 0 ) : 10
 element 2 ( index 1 ) : 9
 element 3 ( index 2 ) : 8
 element 4 ( index 3 ) : 7
 element 5 ( index 4 ) : 6
 element 6 ( index 5 ) : 6
 element 7 ( index 6 ) : 7
 element 8 ( index 7 ) : 8
 element 9 ( index 8 ) : 9
 element 10 ( index 9 ) : 10

Solution :


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
Download Broncode

Task :


Create a program that fills an array with Fibonacci numbers.

Fibonacci : 1 1 2 3 5 8 13 21 34 55 89 ...
The first two values are 1, all next values are the sum of the previous two.


Output :

 Count : <i>1</i>
 1

Output :

 Count : <i>2</i>
 1  1

Output :

 Count : <i>3</i>
 1  1  2

Output :

 Count : <i>11</i>
 1  1  2  3  5  8  13  21  34  55  89

Solution :


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
Download Broncode

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 ?
 <i>-1</i>
 Value ?
 <i>4000</i>
 Value ?
 <i>1</i>
 I
 Value ?
 <i>3999</i>
 MMMCMXCIX
 Value ?
 <i>2789</i>
 MMDCCLXXXIX
 Value ?

Solution :


Module Exercise8Solution
    Sub Main()
        Dim symbols() As String = _
         {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
        Dim units() As String = _
                          {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




This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-01-23

Download Broncode

Published On : 2008-06-24

Introduction to Arrays

Vorig Onderwerp

Constants

|

Multidimensional Arrays

Volgend Onderwerp

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).