19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXAMPLE 5<br />

In the following program the array firm( ) contains the alphabetized names of up to 100 corporations. The<br />

program requests the name of a corporation as input <strong>and</strong> uses a binary search to determine whether or not<br />

the corporation is in the array.<br />

Dim firm(1 TO 100) As String<br />

Dim numFirms As Integer<br />

Private Sub BinarySearch(corp As String, result As String)<br />

Dim foundFlag As Boolean<br />

Dim first As Integer, middle As Integer, last As Integer<br />

‘Array firm() assumed already ordered alphabetically<br />

‘Binary search of firm() for corp<br />

foundFlag = False<br />

first = 1<br />

last = numFirms<br />

Do While (first corp<br />

last = middle - 1<br />

Case Is <<br />

corp first = middle + 1<br />

End Select<br />

Loop<br />

If foundFlag Then<br />

result = “found”<br />

Else<br />

result = “not found”<br />

End If<br />

End Sub<br />

Private Sub cmdSearch_Click()<br />

Dim corp As String, result As String<br />

corp = UCase(Trim(txtCorporation.Text))<br />

Call BinarySearch(corp, result)<br />

‘Display results of search<br />

picResult.Cls<br />

picResult.Print corp; “ ”; result<br />

End Sub<br />

Private Sub Form_Load()<br />

‘Fill array with data from FIRMS.TXT<br />

Open “FIRMS.TXT” For Input As #1 ‘Contains up to 100 companies<br />

numFirms = 0<br />

Do While (Not EOF(1)) And (numFirms < UBound(firm))<br />

numFirms = numFirms + 1<br />

Input #1, firm(numFirms)<br />

Loop Close #1<br />

End Sub<br />

Sorting <strong>and</strong> Searching 193

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!