Public Class Tester
Public Shared Sub Main
Dim array1 As Integer() = New Integer(19) {}
Dim randomNumber As Random = New Random()
Dim i As Integer
' creates string containing 11 random numbers
For i = 0 To array1.GetUpperBound(0)
array1(i) = randomNumber.Next(1000)
Console.Write ( array1(i) & " ")
Next
Console.WriteLine("")
Dim searchKey As Integer = 12
Dim element As Integer = LinearSearch(searchKey, array1)
If element <> -1 Then
Console.WriteLine("Found Value in index " & element)
Else
Console.WriteLine("Value Not Found")
End If
End Sub
Shared Function LinearSearch(ByVal key As Integer, _
ByVal numbers As Integer()) As Integer
Dim n As Integer
' structure iterates linearly through array
For n = 0 To numbers.GetUpperBound(0)
If numbers(n) = key Then
Return n
End If
Next
Return -1
End Function ' LinearSearch
End Class
|