School of Business Eastern Illinois University TwoDimensional Arrays
School of Business Eastern Illinois University Two-Dimensional Arrays (Week 11, Friday 4/04/2003) © Abdou Illia, Spring 2003
2 Two-dimensional Array? n One-dimensional arrays have been discussed so far: Tables with only one row – Score (1) 10 n Score (2) 20 Score (3) 0 …… …. . Score (30) 80 Two-dimensional arrays are used to hold contents of: – tables with several rows and columns Chicago Los Angeles New York Philadelphia Chicago 0 2054 802 738 2054 0 2786 2706 Los Angeles 802 2786 0 100 New York 738 2706 Philadelphia 100 0 Two subscripts instead of one
Using Two-dimensional Arrays in VB n Same rules than One-dimensional arrays 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. 3
Declare Two-dimensional Array variables n 4 Use Dim statements with the following syntax Dim Array. Name (First. Row To Last. Row, First. Colomn To Last. Column) 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 Chicago 0 2054 802 738 2054 0 2786 2706 Los Angeles 802 2786 0 100 New York 738 2706 Philadelphia 100 0
Declare Two-dimensional Array variables n 5 Each element of a Two-dimensional array has the form: Array. Name (Row, Column) n Examples: Distances(1, 1) = 0 Distances(1, 3) = 802 Chicago Los Angeles New York Philadelphia Chicago 738 0 802 2054 0 2786 2706 Los Angeles 802 2786 0 100 New York 738 2706 Philadelphia 100 0
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 6 DISTANCE. TXT, assign them to all subscripted variables (elements of the array), and display these values in a picture box
Assign values to subscripted variables n Read the values assign them to all subscripted variables (To be finished in class) Dim Distance. Array(1 To 4, 1 To 4) As Single Private Sub cmd. Read. Assign_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 7
Assign values to subscripted variables n Display these values in a picture box (To be finished in class) Dim Distance. Array(1 To 4, 1 To 4) As Single Private Sub cmd. Read. Assign_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
9 Problem 2: Write a procedure to perform the following task: Given an array dimensioned with the statement Dim Table 1(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. (To be finished in class) n Private Sub cmd. Sum_Click( ) Dim col As Integer, sum As Single 'Add up the elements in the 10 th column sum = 0 For row = 1 To 10 Next row End Sub
10 Problem 3: Write a procedure to perform the following task: Given an array dimensioned with the statement Dim Table 2(1 To 3, 1 To 45) As Single, and values assigned to each element, find the greatest value. (To be finished in class) n Private Sub cmd. Greatest_Click( ) Dim row As Integer, col As Integer Dim greatest As Single 'Find greatest value in array What should Condition be? Greatest = Table(1, 1) For row = 1 To 3 For col = 1 To 45 If Condition Then End If Next col Next row pic. Output. Print "The greatest value is"; Greatest End Sub ?
- Slides: 10