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.

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

‘Analyze exam scores<br />

total = 0<br />

For student = 1 To 8<br />

total = total + score(student)<br />

Next student<br />

average = total / 8<br />

‘Display all names with above-average grades<br />

picTopStudents.Cls<br />

For student = 1 To 8<br />

If score(student) > average Then<br />

picTopStudents.Print nom(student)<br />

End If<br />

Next student<br />

End Sub<br />

[Run, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

FIGURE 6-3 Arrays Created by Example 3<br />

In Example 3, the number of students to be processed had to be known at the time the<br />

program was written. In actual practice, the amount of data that a program will be processing<br />

is not known in advance. Programs should be flexible <strong>and</strong> incorporate a method for h<strong>and</strong>ling<br />

varying amounts of data. <strong>Visual</strong> <strong>Basic</strong> makes this possible with the statement<br />

ReDim arrayName (1 to n) As varType<br />

which can use variables or expressions when indicating the subscript range. However, ReDim<br />

statements can only be used inside procedures.<br />

EXAMPLE 4<br />

The following program reworks Example 3 for the case when the amount of data is not known in advance.<br />

Private Sub cmdShow_Click()<br />

Dim numStudents As Integer, nTemp As String, sTemp As Integer<br />

Dim student As Integer, total As Integer, average As Single

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

Saved successfully!

Ooh no, something went wrong!