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.

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

2. In Example 3, parallel arrays already ordered by one field were sorted by another<br />

field. Usually, parallel arrays are sorted by the field to be searched when<br />

accessing the file. This field is called the key field.<br />

3. Suppose an array of 2000 items is searched sequentially—that is, one item after<br />

another—in order to locate a specific item. The number of comparisons would<br />

vary from 1 to 2000, with an average of 1000. With a binary search, the number<br />

of comparisons would be at most 11 because 211 > 2000.<br />

4. The built-in function UCase converts all the characters in a string to uppercase.<br />

UCase is useful in sorting <strong>and</strong> searching arrays of strings when the alphabetic<br />

case (upper or lower) is unimportant. For instance, Example 5 includes UCase<br />

in the Select Case comparisons, <strong>and</strong> so the binary search will locate “Mobil” in<br />

the array even if the user entered “MOBIL”.<br />

5. The <strong>Visual</strong> <strong>Basic</strong> function Timer can be used to determine the speed of a sort. Precede<br />

the sort with the statement t = Timer. After the sort has executed, the statement<br />

picOutput.Print Timer – t will display the duration of the sort in seconds.<br />

6.5 TWO-DIMENSIONAL ARRAYS<br />

Each array discussed so far held a single list of items. Such array variables are called singlesubscripted<br />

variables. An array can also hold the contents of a table with several rows <strong>and</strong><br />

columns. Such arrays are called two-dimensional arrays or double-subscripted variables.<br />

Two tables follow. Table 6.4 gives the road mileage between certain cities. It has four rows <strong>and</strong><br />

four columns. Table 6.5 shows the leading universities in three disciplines. It has three rows<br />

<strong>and</strong> five columns.<br />

TABLE 6.4<br />

Road Mileage Between Selected U.S. Cities<br />

Chicago Los Angeles New York Philadelphia<br />

Chicago 0 2054 802 738<br />

Los Angeles 2054 0 2786 2706<br />

New York 802 2786 0 100<br />

Philadelphia 738 2706 100 0<br />

TABLE 6.5<br />

University Rankings<br />

1 2 3 4 5<br />

Business U of PA U of IN U of MI UC Berk U of VA<br />

Comp Sci. MIT Cng-Mellon UC Berk Cornell U of IL<br />

Engr/Gen. U of IL U of OK U of MD Cng-Mellon CO Sch. of Mines<br />

Source: A Rating of Undergraduate Programs in American <strong>and</strong> International Universities, Dr. Jack<br />

Gourman, 1998<br />

Two-dimensional array variables store the contents of tables. They have the same types<br />

of names as other array variables. The only difference is that they have two subscripts, each<br />

with its own range. The range of the first subscript is determined by the number of rows in<br />

the table, <strong>and</strong> the range of the second subscript is determined by the number of columns. The<br />

statement

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

Saved successfully!

Ooh no, something went wrong!