How to Find the Maximum Value in an Array in Visual Basic


Arrays are variables used in programming to hold a list of numbers or characters. Finding the maximum number value in an array requires you to loop through each value. This is also used in a method called "bubble sorting," which means each value is compared and listed in increasing order. You can use a bubble sort to find the maximum value for an array in Visual Basic.

  • Initialize the variables needed. There are three variables needed to loop through the array to find the highest value: the array that contains the values, a temporary variable to loop through each array value and a variable to hold the highest value. These variables are initialized using the following code:

    Dim myArray (2) as Integer
    Dim i as Integer
    Dim maxValue as Integer
    myArray(0) = 1
    myArray(1) = 2
    myArray(2) = 3

    Notice the array is initialized with numbers, 1, 2 and 3. After the code is finished executing, the maximum value shown is 3.

  • Obtain the first value in the array and set it to the maxValue variable. This variable is used to compare each array value. If the next value in the array list is higher than the current value, then maxValue holds the maximum number. The code is below:

    maxValue = myArray(0)

  • Loop through each value in the array to find the maximum value. The code is below:

    For i = 0 To myArray.GetUpperBound() - 1

    If maxValue < myArray(i) Then
    maxValue = myArray(i)
    End If


  • Print the results to the debugger window. This is used to test your code syntax and logic for any errors. The following code prints "3" after the loop is finished:

    Print maxValue

Related Searches


  • Photo Credit Comstock Images/Comstock/Getty Images
Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!