0

I am currently working on an application where the user inputs 10 integers into an array and then clicks a button to display the integers in ascending or descending order from highest to lowest, however it is not working as intended. When I click the button that should sort them, nothing happens. Can anyone give some insight? Here is my current code.

 Function intInput() As Integer
    Const intMax As Integer = 9
    Dim intArray(intMax) As Integer
    Dim intCount As Integer 'Loop counter

    For intCount = 0 To intMax 'Run intCount through intMax 10 times
        intArray(intCount) = CInt(InputBox("Please enter an integer: ")) 'input the integers and store them into the array element
    Next
    lstArray.Items.Clear()

    For intCount = 0 To intMax
        lstArray.Items.Add(intArray(intCount)) 'add the integers to the list
    Next
    Return intArray(intCount)
End Function

Function intHighest() As Integer
    Const intMax As Integer = 9
    Dim intArray(intMax) As Integer
    Dim intCount As Integer 'Loop counter
    Dim intHigh As Integer


    intHigh = intArray(0)

    For intCount = 1 To (intArray.Length - 1)
        If intArray(intCount) > intHigh Then
            intHigh = intArray(intCount)
        End If
    Next
    Return intHigh
End Function

Function intLow() As Integer
    Const intMax As Integer = 9
    Dim intArray(intMax) As Integer
    Dim intCount As Integer 'Loop counter
    Dim intLowest As Integer


    intLowest = intArray(9)

    For intCount = 1 To (intArray.Length - 1)
        If intArray(intCount) < intLowest Then
            intLowest = intArray(intCount)
        End If
    Next
    Return intLowest
End Function

Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
    intInput()

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    lstArray.Items.Clear()
End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    intHighest()
    intLow()


End Sub

End Class

4
  • 1
    I cant see any code that sorts or displays Commented Jun 3, 2015 at 22:21
  • 1
    And redeclaring the array inside the intHighest and intLow doesn't help too Commented Jun 3, 2015 at 22:23
  • What would be your suggested approach? I'm going through my book to try to find where I went wrong but would appreciate advice from those more experienced than I. Commented Jun 3, 2015 at 22:28
  • You need to learn about Scope. The intArrays you declare inside subs only exists there. You need a module level array var. Your hi-low procs are evaluating an empty array. Set a breakpoint and see. Commented Jun 3, 2015 at 22:47

1 Answer 1

1

There is an easier way to find the minimum and maximum values in an array, and to sort the values in an array.

Dim arrNum() As Integer = {3, 1, 2} 'Create an integer array
Dim minNum As Integer = arrNum.Min 'Get the minimum value
Dim maxNum As Integer = arrNum.Max 'Get the maximum value
Array.Sort(arrNum) 'Sort the array
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.