jeudi 14 décembre 2017

I am trying to implement a linear search in VB.net but I keep on getting "Index was outside the bounds of the array" error

I am trying to implement a linear search but whenever i move on to the linearSearch() subroutine, I get the error above. The line giving this error is the one containing "If list(a) = numberToFind Then". How can I fix this?

Module Module1

Sub Main()

    Dim list(99) As Integer
    Dim x As Integer = 0
    Dim answer As Integer

    Console.Write("Enter a value, type 9999 to stop.")
    answer = Console.ReadLine()

    For i = 0 To list.Length

        If answer = 9999 Then
            linearSearch(list)
        Else
            list(i) = answer
            Console.Write("Enter another")
            answer = Console.ReadLine

        End If
    Next

End Sub

Sub linearSearch(ByVal list)

    Dim numberToFind, comparisonNo As Integer
    comparisonNo = 0

    Console.Write("What number do you want to find?")
    numberToFind = Console.ReadLine()

    For a = 1 To list.Length
        If list(a) = numberToFind Then
            Console.Write(comparisonNo)
        Else
            comparisonNo += 1
        End If
    Next
    Console.ReadLine()
End Sub

End Module

Aucun commentaire:

Enregistrer un commentaire