CrazyEngineers
  • Vb 6.0

    samaira

    Member

    Updated: Oct 26, 2024
    Views: 1.1K
    I have done the following code for binary search.But all the time Its showing match not found even if the number is present in the array😔

    Dim a As Integer
    Dim i, j, k As Integer
    Dim numbers(20) As Integer

    Private Sub Command1_Click()
    j = InputBox(" enter the number of elements in the array")
    For i = 0 To j - 1
    k = InputBox("enter the number:")
    numbers(i) = k
    Next i
    MsgBox ("numbers successfully inserted")
    a = InputBox("enter the number to be searched")
    search12 (a)
    End Sub

    Private Function search12(b As Integer)
    Dim Min As Integer
    Dim Max As Integer
    Dim Mid As Integer
    Min = 0
    Max = UBound(numbers)
    Do While Min <= Max
    Mid = Int((Min + Max) / 2)
    If b = numbers(Mid) Then
    ' we have a match
    MsgBox ("match found")
    Exit Function
    ElseIf b < numbers(Mid) Then
    Max = Mid - 1
    ElseIf b > numbers(Mid) Then
    Min = Mid + 1
    End If
    Loop
    'If (Min > Max) Then
    ' if we get here, the state was not found ...
    MsgBox ("match not found")
    'End If
    Exit Function
    End Function
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberOct 16, 2010

    Can you post any example input for this program?
    Here i mean array
    Are you sure? This action cannot be undone.
    Cancel
  • whiz.kid.aniket

    MemberOct 16, 2010

    @samaira are you using ActiveX dll?
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberOct 17, 2010

    whiz.kid.aniket
    @samaira are you using ActiveX dll?
    no m not using activex dll
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberOct 17, 2010

    goyal420
    Can you post any example input for this program?
    Here i mean array
    like e.g i gave input as 1,2,3,4,5,6 for n=6 and searched for 3
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberOct 17, 2010

    Do 1 thing
    remove last condition
    and add print statement or whatever used in vb to display output in order to display value of mid and tell me the output
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register