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.

End Sub<br />

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

We could have written the program in Example 2 with a For...Next loop beginning For<br />

n = 1 To 5. However, such a loop would unnecessarily search the entire list when the soughtafter<br />

item is found early. The wasted time could be significant for a large array.<br />

In some applications, arrays are needed only temporarily to help a procedure complete<br />

a task. <strong>Visual</strong> <strong>Basic</strong> also allows us to create array variables that are local to a specific procedure<br />

<strong>and</strong> that exist temporarily while the procedure is executing. If the statement<br />

Dim arrayName(1 To n) As varType<br />

is placed inside an event procedure or general procedure, then space for n subscripted variables<br />

is set aside in memory each time the procedure is invoked <strong>and</strong> released when the procedure is<br />

exited.<br />

In Example 1, values were assigned to the elements of the array with assignment statements.<br />

However, data for large arrays are more often stored in a data file <strong>and</strong> read with Input<br />

# statements. Example 3 uses this technique. Also, because the task of the program is likely<br />

to be performed only once during a run of the program, a local array is used.<br />

EXAMPLE 3<br />

Table 6.1 gives names <strong>and</strong> test scores from a mathematics contest given in 1953. Write a program to display<br />

the names of the students scoring above the average for these eight students.<br />

TABLE 6.1<br />

The Top Scores on the Fourth Annual Mathematics Contest Sponsored by the<br />

Metropolitan NY Section of the MAA<br />

Richard Dolen 135 Paul H. Monsky 150<br />

Geraldine Ferraro 114 Max A. Plager 114<br />

James B. Fraser 92 Robert A. Schade 91<br />

John H. Maltby 91 Barbara M. White 124<br />

Source: The Mathematics Teacher, February 1953.<br />

SOLUTION:<br />

The following program creates a string array to hold the names of the contestants <strong>and</strong> a numeric array to<br />

hold the scores. The first element of each array holds data for the first contestant, the second element of<br />

each array holds data for the second contestant, <strong>and</strong> so on. See Figure 6-3. Note that the two arrays can<br />

be dimensioned in a single Dim statement by placing a comma between the array declarations.<br />

Private Sub cmdShow_Click()<br />

Dim total As Integer, student As Integer, average As Single<br />

’Create arrays for names <strong>and</strong> scores<br />

Dim nom(1 To 8) As String, score(1 To 8) As Integer<br />

‘Assume the data has been placed in the file “SCORES.TXT”<br />

‘(The first line of the file is “Richard Dolen”, 135)<br />

Open “SCORES.TXT” For Input As #1<br />

For student = 1 To 8<br />

Input #1, nom(student), score(student)<br />

Next student<br />

Close #1<br />

Creating <strong>and</strong> Accessing Arrays 165

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

Saved successfully!

Ooh no, something went wrong!