user3343463
user3343463

Reputation: 5

Displaying Greater and Smaller value

Could someone point out what's wrong with this code. it's not giving me correct max and min value from five input numbers.. (Using IF..)

    Dim arr() As Integer = {1, 2, 3, 4, 5}
    Dim max As Integer = arr(0)
    Dim min As Integer = arr(0)
    arr(0) = InputBox("Enter 1st Value")
    arr(1) = InputBox("Enter 2nd Value")
    arr(2) = InputBox("Enter 3rd Value")
    arr(3) = InputBox("Enter 4th Value")
    arr(4) = InputBox("Enter 5th Value")
    For i As Integer = 1 To arr.Length - 1
        If arr(i) > max Then
            max = arr(i)
        End If
        If arr(i) < min Then
            min = arr(i)
        End If
        TextBox1.Text = max
        TextBox2.Text = min
    Next

Upvotes: 0

Views: 43

Answers (1)

paxdiablo
paxdiablo

Reputation: 881573

For a start, you probably want to set min and max after you have entered the first element of the array.

For example, you currently set min to 1 since that's the first element in the array before you start data entry.

If you then enter five values all greater than a hundred, min will remain as 1 despite the fact you didn't enter it.

You're probably looking for something like:

Dim arr(4) As Integer // 0-4 inclusive (from memory)

Dim max As Integer
Dim min As Integer

arr(0) = InputBox("Enter 1st Value")
arr(1) = InputBox("Enter 2nd Value")
arr(2) = InputBox("Enter 3rd Value")
arr(3) = InputBox("Enter 4th Value")
arr(4) = InputBox("Enter 5th Value")

max = arr(0)
min = arr(0)

For ...

Upvotes: 1

Related Questions