Linear Search Array Error
I am trying to implement a linear search but whenever i move on to the linearSearch()
subroutine, I get the error:
Индекс находился вне границ массива
The line giving this error is the one containing If list(a) = numberToFind Then
, Как я могу это исправить?
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
1 ответ
Измените строки "Для a = 1 To list.Length" на "Для a = 0 To list.Length - 1". Массивы основаны на нуле.