0

I've written this code to sort an array of 5 numbers in ascending order but I've got an error:

A first chance exception of type 'System.IndexOutOfRangeException' occurred in ConsoleApplication1.exe

Here is the code:

Module Module1
Dim numbers(5) As Integer
Dim flag As Boolean
Dim i As Integer = 0

Sub InputNumbers()

    For i = 0 To 4
        Console.WriteLine("Input Numbers ")
        numbers(i) = Console.ReadLine()
    Next i
End Sub

Sub Sort()
    Dim temp As Integer
    Do
        flag = False
        For i = 0 To 4
            If numbers(i) > numbers(i + 1) Then
                temp = numbers(i + 1)
                numbers(i + 1) = numbers(i)
                numbers(i) = temp
            End If
        Next i
    Loop Until flag = True

End Sub

Sub Output()
    For i = 0 To 4
        Console.WriteLine("The result is : " & numbers(i))
    Next i


End Sub

Sub Main()
    InputNumbers()
    Sort()
    Output()

    Console.ReadKey()
End Sub

The error is found here:

For i = 0 To 4
    If numbers(i) > numbers(i + 1) Then
        temp = numbers(i + 1)
        numbers(i + 1) = numbers(i)
        numbers(i) = temp
    End If
Next i

Can someone please help?

1
  • You appear to have forgotten to set flag appropriately. Commented May 21, 2016 at 14:05

2 Answers 2

2

Your array actually contains 6 elements.

'5 represents the upper bond (0 to 5)
Dim numbers(5) as integer 

'Declare a single-dimension array of 5 values
Dim numbers(4) As Integer 

Then, your statement below is wrong

For i = 0 To 4
        If numbers(i) > numbers(i + 1) Then
            temp = numbers(i + 1)
            numbers(i + 1) = numbers(i)
            numbers(i) = temp
        End If
    Next i

In general, to avoid breaking your code if you ever change the array size, I would use GetUpperBound to get the last index of your array. Furthermore, you cannot make a for to loop up to the last element since in your loop, you look at index: i+1 which will give you an index out of range exception on the last element (That is why I added the "-1" after GetUpperbound.

For i = 0 To numbers.GetUpperBound(0) -1
        If numbers(i) > numbers(i + 1) Then
            temp = numbers(i + 1)
            numbers(i + 1) = numbers(i)
            numbers(i) = temp
        End If
Next

Ultimately however, the most efficient way to sort your array without hassle is to do this.

Array.Sort(numbers)

For your output function, I would use either GetUpperbound instead of 4 (which will be problematic if the array size is either changed and you forget to change the number) or a For each statement that will adapt itself to any array size without changing that part of the code.

Sub Output()
For each i as integer in numbers
    Console.WriteLine("The result is : " & numbers(i))
Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

As Sage Pourpre pointed out, where you are using i + 1 to reference the next element in the array, you have to make sure that i + 1 is not greater than the last index of the array. There is more than one way to do that.

Also, with the code in your question, you will have an unending loop because you haven't set the value of flag appropriately. I suggest naming that variable isSorted because it is more meaningful:

Sub Sort()
    ' perform a bubble sort
    Dim temp As Integer
    Dim isSorted As Boolean

    Do
        isSorted = True
        For i = 0 To numbers.Length - 2
            If numbers(i) > numbers(i + 1) Then
                temp = numbers(i + 1)
                numbers(i + 1) = numbers(i)
                numbers(i) = temp
                isSorted = False
            End If
        Next i
    Loop Until isSorted

End Sub

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.