codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Binary Search Question  jjordan354 at 21:42 on Thursday, April 08, 2004
 

The following program does a binary search for a zip code from a text file. When the search is performed, then first record that is found, is returned, but i need the search to continue to find more of the same zip code that was entered. If and where do i put the loop to do so? THanks in advance.
Jim

Option Explicit
Dim zip() As String
Dim city() As String
Dim county() As String
Dim state() As String
Dim intnum As Double

Private Sub cmdsearchzip_Click()
Dim searchzip As String, result As Double

Label2.Caption = result
Call getinfo(searchzip)
Call findinfo(searchzip, result)

If result > 0 Then
Call showdata(result)
Else
lbloutput.Caption = "Zip not found!!"
End If


End Sub
'Private Sub findinfo(searchzip as String nt as String)

Private Sub Form_Load()
Dim i As Double
Dim zip1 As String
Dim city1 As String
Dim county1 As String
Dim state1 As String


Open "/forum/ctempZipCodes.txt" For Input As #1
Do While Not EOF(1)
Input #1, zip1, city1, county1, state1
intnum = intnum + 1
Loop
Close #1
ReDim zip(1 To intnum) As String, city(1 To intnum) As String
ReDim county(1 To intnum) As String, state(1 To intnum) As String
Open "/forum/ctempZipCodes.txt" For Input As #1

For i = 1 To intnum
Input #1, zip(i), city(i), county(i), state(i)
Next i
Close #1

End Sub



Private Sub txtcity_GotFocus()
cmdsearchzip.Visible = False

End Sub

Private Sub findinfo(searchzip As String, result As Double)
Dim foundflag As Boolean
Dim first As Double, middle As Double, last As Double
foundflag = False
first = 1
last = intnum
Do While (first <= last) And (Not foundflag)
middle = Int((first + last) / 2)
Label4.Caption = middle
Select Case zip(middle)
Case searchzip
foundflag = True
Case Is > searchzip
last = middle - 1
Case Is < searchzip
first = middle + 1
End Select
Loop
If foundflag Then
result = middle
Else
result = 0
End If


End Sub

Private Sub getinfo(searchzip As String)
searchzip = txtzip.Text
End Sub

Private Sub showdata(result As Double)
picout.Print "City(s) "; city(result)
picout.Print "County(s): " & county(result)
picout.Print "State(s): " & state(result)

End Sub




  Re: Binary Search Question  jkluce at 05:46 on Friday, April 09, 2004
 

2 things are keeping you from seeing all the matches.

1st - You are only finding the first occurrance of the zip becuase of the Do While (first <= last) And (Not foundflag) Take this out and you will search thru the whole thing.

2nd - You only output at the very end of your program. Move the output to when you get a hit (the case statement), then you will see the multiple hits. If you cannot do this, then store the hits in an result() array, then loop thru that array at the end displaying the hits.


  Re: Binary Search Question  jjordan354 at 16:15 on Friday, April 09, 2004
 

removing the not found falg did not work, but it created an ifinite loop. :o) I figured out how to display more than one zip by taking the first zip i find and comparing it the the next zip i search for and that worked just fine. Now i need to figure out how to do a shell sort on a 2 dimensional array. I got a book in front of me that tells me how to do a shell sort on a single dimension array.

<Added>

*Infinite*

  Re: Binary Search Question  jkluce at 21:14 on Friday, April 09, 2004
 

DOH! Sorry... That makes sense... when you hit a zip that matches, you need to change first and last to avoid the infinite loop.

I've always been good at infinite loops.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)
•  IPC problem
•  Re: import contacts of msn/yahoo
•  Cookies and Threads C++
•  right justify a background in a table?
•  Help with Loop (C++/MFC)
•  Help with Loop (C++/MFC)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007