954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

binary search difficulty

I am having trouble with this code and realizing that my flags are not work.
I am referring to a list box with US states inside.
See my code below and tell me what I am doing wrong. I can not output the state with first two letters.
A simple concept but I can not solve.
VB2010

Public Class frmStates

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        
        
        Dim letter As String = mtbFirstTwo.Text.ToUpper
        Dim Flag As Boolean = True
        Dim state As String = ""

        Dim first, mid, last As Integer
        first = 0
        last = CInt(lstStates.Items.Count) - 1

        Do While (Not Flag) And (first <= last)
            mid = CInt((first + last)) / 2
            Select Case CStr(lstStates.Items(mid)).Substring(0, 2).ToUpper
                Case letter
                    state = CStr(lstStates.Items(mid))
                    Flag = True
                Case Is > letter
                    last = mid - 1

                Case Is < letter
                    first = mid + 1

            End Select
        Loop
        If Flag Then
            txtOutput.Text = state & " begins with " & mtbFirstTwo.Text & "." 'can not get output
        Else

            txtOutput.Text = "No State begins with " & mtbFirstTwo.Text & "."
        End If
    
    End Sub
thetechlady21
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

You need to initialize the variable "Flag" to False.

If you look at how a Do...While works, it is looking for the entire "While" condition to evaluate to "True". Since Flag = True, then (Not Flag) = False. And we know that your initial values for variable "first" is less than variable "last", so that evaluates to True. So from the truth tables, False AND True = False, therefore your entire condition statement evaluates to False, and the Do loop never executes.

Hope this helps!

BitBlt
Master Poster
711 posts since Feb 2011
Reputation Points: 367
Solved Threads: 109
 

You need to initialize the variable "Flag" to False.

If you look at how a Do...While works, it is looking for the entire "While" condition to evaluate to "True". Since Flag = True, then (Not Flag) = False. And we know that your initial values for variable "first" is less than variable "last", so that evaluates to True. So from the truth tables, False AND True = False, therefore your entire condition statement evaluates to False, and the Do loop never executes.

Hope this helps!

Thank you for this feedback. However, I changed my flag to false to execute the loop. However my variable 'states' does not seem to show as an output. Any ideas? The error shows that the value is null and been used before. Maybe it is my version of the program.

thetechlady21
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

I don't see a variable named "states". Sounds like a different problem. Post your code and the error.

BitBlt
Master Poster
711 posts since Feb 2011
Reputation Points: 367
Solved Threads: 109
 
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim letter As String = txtFirstTwoLetters.Text.ToUpper
        Dim foundflag As Boolean = False
        Dim usstates As String
        Dim first, middle, last As Integer
        first = 0
        last = lstStates.Items.Count - 1
        Do While Not foundflag And (first <= last)
            middle = CInt((first + last) / 2)
            Select Case CStr(lstStates.Items(middle)).Substring(0, 2).ToUpper
                Case letter
                    usstates = CStr(lstStates.Items(middle))
                    foundflag = True
                Case Is > letter
                    last = middle - 1
                Case Is < letter
                    first = middle + 1

            End Select
        Loop
        If foundflag Then
            txtOutput.Text = usstates & " begins with " & txtFirstTwoLetters.Text & "."

        Else
            txtOutput.Text = "No state begins with " & txtFirstTwoLetters.Text & "."
        End If
    End Sub
End Class
I don't see a variable named "states". Sounds like a different problem. Post your code and the error.


I Have changed the variable to usstates, am I missing a list box setting.

thetechlady21
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Put your cursor on the Do While statement.
Press F9
Run the program.
When it stops, look a the values of the variables. Are they correct?
If not, check to code to see why not, and hit STOP
If so, < press F8, check variables, repeat > until you find the error.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: