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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

194 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Run, type IBM into the text box, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

Suppose the array contains 100 corporations <strong>and</strong> the corporation input in Example 5 is in<br />

the second half of the array. On the first pass, middle would be assigned Int((1 + 100)/2) =<br />

Int(50.5) = 50 <strong>and</strong> then first would be altered to 50 + 1 = 51. On the second pass, middle would<br />

be assigned Int((51 + 100)/2) = Int(75.5) = 75. If the corporation is not the array element with<br />

subscript 75, then either last would be assigned 74 or first would be assigned 76, depending<br />

on whether the corporation appears before or after the 75th element. Each pass through the<br />

loop halves the range of subscripts containing the corporation until the corporation is located.<br />

In Example 5, the binary search merely reported whether or not an array contained a certain<br />

item. After finding the item, its array subscript was not needed. However, if related data<br />

are stored in parallel arrays (as in Table 6.2), the subscript of the found item can be used to<br />

retrieve the related information in the other arrays. This process, called a table lookup, is<br />

used in the following example.<br />

EXAMPLE 6<br />

Use a binary search procedure to locate the data for a city from Example 3 requested by the user.<br />

SOLUTION:<br />

The following program does not include a sort of the data file CITYSTAT.TXT because the file is already<br />

ordered alphabetically.<br />

Dim city(1 To 10) As String, pop(1 To 10) As Single, income(1 To 10) As Single<br />

Dim natives(1 To 10) As Single, advDeg(1 To 10) As Single<br />

Private Sub cmdDisplayStats_Click()<br />

Dim searchCity As String, result As Integer<br />

‘Search for city in the metropolitan areas table<br />

Call GetCityName(searchCity)<br />

Call FindCity(searchCity, result)<br />

picResult.Cls<br />

If result > 0 Then<br />

Call ShowData(result)<br />

Else<br />

picResult.Print searchCity & “ not in file”<br />

End If<br />

End Sub<br />

Private Sub FindCity(searchCity As String, result As Integer)<br />

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

Dim foundFlag As Boolean<br />

‘Binary search table for city name<br />

first = 1<br />

last = 10<br />

foundFlag = False<br />

Do While (first searchCity

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

Saved successfully!

Ooh no, something went wrong!