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.

6.1 CREATING AND ACCESSING ARRAYS<br />

A variable (or simple variable) is a name to which <strong>Visual</strong> <strong>Basic</strong> can assign a single value. An<br />

array variable is a collection of simple variables of the same type to which <strong>Visual</strong> <strong>Basic</strong> can<br />

efficiently assign a list of values.<br />

Consider the following situation. Suppose you want to evaluate the exam grades for 30<br />

students. Not only do you want to compute the average score, but you also want to display<br />

the names of the students whose scores are above average. You might place the 30 pairs of<br />

student names <strong>and</strong> scores in a data file <strong>and</strong> run the program outlined.<br />

Private Sub cmdButton_Click()<br />

Dim student1 As String, score1 As Single<br />

Dim student2 As String, score2 As Single<br />

Dim student3 As String, score3 As Single<br />

.<br />

.<br />

Dim student30 As String, score30 As Single<br />

’Analyze exam grades<br />

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

Input #1, student1, score1<br />

Input #1, student2, score2<br />

Input #1, student3, score3<br />

.<br />

.<br />

Input #1, student30, score30<br />

‘Compute the average grade<br />

.<br />

.<br />

‘Display names of above average students<br />

.<br />

.<br />

End Sub<br />

This program is going to be uncomfortably long. What’s most frustrating is that the 30 Dim<br />

statements <strong>and</strong> 30 Input # statements are very similar <strong>and</strong> look as if they should be condensed<br />

161

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

Saved successfully!

Ooh no, something went wrong!