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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

dimensions an array of type varType corresponding to a table with rows labeled from m1 To<br />

n1 <strong>and</strong> columns labeled from m2 To n2. The entry in the jth row, kth column is arrayName (j,k).<br />

For instance, the data in Table 6.4 can be stored in an array named rm( ). The statement<br />

Dim rm(1 To 4, 1 To 4) As Single<br />

will dimension the array. Each element of the array has the form rm(row, column). The entries<br />

of the array are<br />

rm(1,1)=0 rm(1,2)=2054 rm(1,3)=802 rm(1,4)=738<br />

rm(2,1)=2054 rm(2,2)=0 rm(2,3)=2786 rm(2,4)=2706<br />

rm(3,1)=802 rm(3,2)=2786 rm(3,3)=0 rm(3,4)=100<br />

rm(4,1)=738 rm(4,2)=2706 rm(4,3)=100 rm(4,4)=0<br />

As with one-dimensional arrays, when a two-dimensional array is created using Dim in the<br />

(Declarations) section of (General), the array becomes a form-level subscripted variable, <strong>and</strong> is<br />

therefore accessible in all event procedures <strong>and</strong> general procedures <strong>and</strong> retains whatever values<br />

are assigned until the program is terminated. Two-dimensional arrays also can be created with<br />

Dim that are local to a procedure <strong>and</strong> cease to exist once the procedure is exited. When the<br />

range of the subscripts is given by one or more variables, the proper statement to use is<br />

ReDim arrayName(m1 To n1, m2 To n2) As varType<br />

The data in Table 7.10 can be stored in a two-dimensional string array named univ( ).<br />

The statement<br />

Dim univ(1 To 3, 1 To 5) As String<br />

will dimension the array as form-level. Some of the entries of the array are<br />

univ(1,1) = “U of PA”<br />

univ(2,3) = “UC Berk”<br />

univ(3,5) = “CO Sch. of Mines”<br />

EXAMPLE 1<br />

Write a program to store <strong>and</strong> access the data from Table 6.4.<br />

SOLUTION:<br />

Data are read from the data file DISTANCE.TXT into a two-dimensional form-level array using a pair of<br />

nested loops. The outer loop controls the rows <strong>and</strong> the inner loop controls the columns.<br />

Object Property Setting<br />

frmDist Caption Intercity Distances<br />

lblCh Caption 1. Chicago<br />

lblLA Caption 2. Los Angeles<br />

lblNY Caption 3. New York<br />

lblPh Caption 4. Philadelphia<br />

lblOrig Caption Origin<br />

txtOrig Text (blank)<br />

lblDest Caption Destination<br />

txtDest Text (blank)<br />

cmdShow Caption Show Mileage<br />

between Origin<br />

<strong>and</strong> Destination<br />

picMiles<br />

Dim rm(1 To 4, 1 To 4) As Single ‘In (Declarations) section of (General)<br />

Private Sub cmdShow_Click()<br />

Dim row As Integer, col As Integer<br />

‘Determine road mileage between cities<br />

row = Val(txtOrig.Text)<br />

col = Val(txtDest.Text)<br />

Two-Dimensional Arrays 197

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

Saved successfully!

Ooh no, something went wrong!