Wyo VB Lecture Notes
Objective #1 - How can you use a two-dimensional array to efficiently manage a set of related data?
- A two-dimensional array is an extension of a one-dimensional array. Elements in a two-dimensional array are stored in rows and columns. Two-dimensional arrays are
more convenient and efficient than one-dimensional arrays to store certain sets of data. For example, in board games such as Battleship, Tic Tac Toe, Bingo, Connect 4, Minesweeper, Scrabble, and even
LightBright, a two-dimensional array could be used to keep track of the players' board positions.
- You could use the following statement to declare a two-dimensional array...
Dim mScores(4, 2) As Integer
In this example, assume that the array is being used as a module-level variable.
The m is used in the name to indicate that assumption. The prefix int is
used of course to show that the array stores integers. By parentheses indicate
that mScores is an array but by placing two integer values in the parentheses,
we have indicated that it is a two-dimensional array with 5 rows and 3 columns.
The first value in the parentheses always refers to the number of rows in
the array. The second value always refers to the number of columns. I remember
this by thinking of the type of soda RC Cola, which is the official soft
drink of the Wyo Computer Science Department.
It is a little confusing but because Visual Basic numbers the rows from 0 to 4, there are a total of 5 rows. Likewise, there are a total of 3 columns in this example numbered from 0 to 2. Other computer languages also provide a zero column and a zero row but they usually require that you place the actual number of desired rows and columns in the parentheses of the declaration statement.
- The following declaration statement could be used to initialize a two-dimensional array rather than having VB store the default value 0 in each of the array's elements:
Dim mScores(,) As Integer = {{100, 200, 300}, _
{100, 100, 100}, _
{200, 200, 200}, _
{299, 300, 298}, _
{150, 150, 150}}
This statement could have been typed on one line of code but it is easier to understand the two-dimensional array as a set of rows and columns by breaking it up over several lines of code with the line continuation operator ( _ ). The number of rows and columns does not have to be provided in the set of parentheses since VB can easily count the number of values placed in the sets of curly braces ( { } ). However, a comma must be placed in the set of parentheses for VB to know that it is a two-dimensional array rather than a one-dimensional array.
- The following example of nested For loops allows the user to input values into every position of the two-dimensional array mScores
For row = 0 To 4
For col = 0 To 2
mScores(row, col) = Val(InputBox("Enter the next score:"))
Next
Next
The loop variables row and col are used in the parentheses of the array in order to allow the user to fill every element of the array.
- The following algorithm could be used to compute the sum of the elements in a two-dimensional array
For row = 0 To 4
For col = 0 To 2
sum = sum + mScores(row, col)
Next
Next
The statement in the body of the nested loop is an accumulator statement and keeps the running total in the variable sum. Notice the use of the loop variables row and col in the parentheses. This is a pattern that you will often see with code that uses two-dimensional arrays.
- The following algorithm could be used to find the largest value stored in a two-dimensional array
For row = 0 To 4
For col = 0 To 2
If (mScores(row, col) > max) Then
max = mScores(row, col)
End If
Next
Next
This is really just a linear search but applied to a two-dimensional array. The algorithm goes row-by-row from left to right looking for a value that is larger than the one currently stored in max. When it finds one, it replaces max with the new value.
- The following algorithm could be used to compute the averages
of the values stored in each row of elements in a two-dimensional array and
stores these separate averages into another one-dimensional array named averages.
For row = 0 To 4
sum = 0
For col = 0 To 2
sum = sum + mScores(row, col)
Next
averages(row) = sum / 3
Next
Notice how it is necessary to reset the value of sum to zero in each iteration of the outer loop