Download presentation
Presentation is loading. Please wait.
1
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
2
2 Two-dimensional Array? n One-dimensional arrays have been discussed so far: – Tables with only one row Score (1) 100…..2080 Score (2) Score (3)Score (30) …… n Two-dimensional arrays are used to hold contents of: – tables with several rows and columns Chicago Los Angeles New York Philadelphia ChicagoLos AngelesNew York Philadelphia 0 2054 802 738 2054 0 2786 2706 802 2786 0 100 738 2706 100 0 Two subscripts instead of one
3
3 Using Two-dimensional Arrays in VB 1)Dim statement to declare the array 2)Assignment statements to assign values to the subscripted variables Note: Open and Input statements can be used to read the values to be assigned. n Same rules than One-dimensional arrays
4
4 Declare Two-dimensional Array variables n Use Dim statements with the following syntax Dim ArrayName (FirstRow To LastRow, FirstColomn To LastColumn) As Type Ranges of the Array. Numbers must be whole numbers n Example: Dim Distances (1 To 4, 1 To 4) As Single Chicago Los Angeles New York Philadelphia ChicagoLos AngelesNew York Philadelphia 0 2054 802 738 2054 0 2786 2706 802 2786 0 100 738 2706 100 0
5
5 Declare Two-dimensional Array variables n Each element of a Two-dimensional array has the form: ArrayName (Row, Column) n Examples: Chicago Los Angeles New York Philadelphia ChicagoLos AngelesNew York Philadelphia 0 2054 802 738 2054 0 2786 2706 802 2786 0 100 738 2706 100 0 Distances(1,1) = 0 Distances(1,3) = 802
6
6 Assign values to subscripted variables n Values can be assigned using individual Assignment statements like: Distances (1,4) = 738 Distances (3,2) = 2786 n But most of the time, Open and Input statements are used to read the values to be assigned n Problem 1 : Write a program that read the values in DISTANCE.TXT, assign them to all subscripted variables (elements of the array), and display these values in a picture box
7
7 Assign values to subscripted variables n Read the values assign them to all subscripted variables (To be finished in class) Dim DistanceArray(1 To 4, 1 To 4) As Single Private Sub cmdReadAssign_Click( ) Dim row As Integer, col As Integer Open “A:\DISTANCE.TXT” For Input As #1 For row = 1 To 4 Next row Close #1 End Sub
8
8 Assign values to subscripted variables n Display these values in a picture box (To be finished in class) Dim DistanceArray(1 To 4, 1 To 4) As Single Private Sub cmdReadAssign_Click( ) Dim row As Integer, col As Integer Open “A:\DISTANCE.TXT” For Input As #1 For row = 1 To 4 Next row Close #1 End Sub
9
9 Problem 2: n Write a procedure to perform the following task: Given an array dimensioned with the statement Dim Table1(1 To 10, 1 To 10) As Single, and values assigned to each element, compute the sum of the values in the 10 th column. Private Sub cmdSum_Click( ) Dim col As Integer, sum As Single 'Add up the elements in the 10th column sum = 0 For row = 1 To 10 Next row End Sub (To be finished in class)
10
10 Problem 3: n Write a procedure to perform the following task: Given an array dimensioned with the statement Dim Table2(1 To 3, 1 To 45) As Single, and values assigned to each element, find the greatest value. Private Sub cmdGreatest_Click( ) Dim row As Integer, col As Integer Dim greatest As Single 'Find greatest value in array Greatest = Table(1, 1) For row = 1 To 3 For col = 1 To 45 If Condition Then End If Next col Next row picOutput.Print "The greatest value is"; Greatest End Sub (To be finished in class) What should Condition be? ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.