Two-dimensional array
A two- dimensional (2D) array is like a table or matrix. The element in row x and column y
of MyArray is referred to as MyArray[x, y].
MyArray[row,column]
For example to store the value 5 in the element in the fourth row and second column, we write:
MyArray[4,2] ←5
When we want to access each element of a 1D array, we use a loop to access each element in
turn. When working with a 2D array, we need a loop to access each row. Within each row we
need to access each column. This means we use a loop within a loop (nested loops).
In pseudocode
FOR Row ← 1 TO MaxRows
FOR Column ← 1 TO MaxColumns
…………..
NEXT Column
NEXT Row
Identifier Explanation
MyArray[1..4, 1..6] Table data structure (2D array) to store values
MaxRows The number of rows in the table (4 in this example)
MaxColumns The number of columns in the table (6 in this example)
Row Counter for the row index
Column Counter for the column index
1
Declaring a 2D (2 Dimensional) array
Example:
Six employees of a company are sales staff. We are to store the number of sales made by each
employee over 12 months.
Each row represents a salesperson
Each column represents a month
Sales
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 0 2 3 8 7 4 5 3 9 0
2 4 0 1 7 8 8 3 0 4 9 0 0
3 5 0 2 4 9 7 4 0 5 8 0 2
4 6 7 3 5 7 4 3 1 6 7 1 1
5 7 8 4 6 8 2 0 2 1 6 2 3
6 8 9 6 0 5 4 4 0 9 5 2 4
Pseudocode
DECLARE <IDENTIFIER>: ARRAY[LBR:UBR, LBC:UBC] OF <DATA TYPE>
Rows Columns
s s
VB
DIM <IDENTIFIER>(MAXROW, MAXCOLUMN) AS <DATA TYPE>
Pseudocode VB
DECLARE Sales: ARRAY[1:6, 1:12] OF INTEGER DIM Sales(6, 12) AS INTEGER
2
Initialising a 2D (2 Dimensional) array
Pseudocode
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
Sales [SalesPersonNumber, MonthNumber] ← 0
NEXT MonthNumber
NEXT SalesPersonNumber
VB
FOR SalesPersonNumber =1 TO 6
FOR MonthNumber = 1 TO 12
Sales (SalesPersonNumber, MonthNumber) = 0
NEXT
NEXT
Assigning values to a 2D (2 Dimensional) array
Pseudocode VB
Sales[4,8] ←11 Sales(4, 8) =11
Input values to a 2D (2 Dimensional) array
Pseudocode
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
INPUT Sales[SalesPersonNumber, MonthNumber]
NEXT MonthNumber
NEXT SalesPersonNumber
3
VB
FOR SalesPersonNumber = 1 TO 6
FOR MonthNumber = 1 TO 12
Console.writeline(“Input number of sales”)
Sales(SalesPersonNumber, MonthNumber)= Console.Readline ( )
NEXT
NEXT
Output values from a 2D (2 Dimensional) array
Pseudocode
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
PRINT Sales[SalesPersonNumber, MonthNumber]
NEXT MonthNumber
NEXT SalesPersonNumber
VB
FOR SalesPersonNumber = 1 TO 6
FOR MonthNumber = 1 TO 12
Console.writeline(Sales(SalesPersonNumber, MonthNumber))
NEXT
NEXT
4
Example: 1
Two-dimensional arrays are very useful and a good place to get started is to create your own
version of the game Battleships with a 4 cell by 4 cell grid. See if you can win, or even break
it!
We are modelling the following board using the two dimensional board variable:
0 1 2 3
0 x o o o
1 o o x o
2 o o o o
3 o o o o
Dim x, y As Integer
Dim board(3, 3) As Char
board(0, 0) = "x"
board(0, 1) = "o"
board(0, 2) = "o"
board(1, 0) = "o"
board(1, 1) = "o"
board(1, 2) = "x"
board(2, 0) = "o"
board(2, 1) = "o"
board(2, 2) = "o"
board(2, 0) = "o"
board(2, 1) = "o"
board(2, 2) = "o"
For z = 1 To 3
Console.WriteLine("This is guess number " & z)
Console.Write("please insert you x location:")
x = Console.ReadLine()
Console.Write("please insert you y location:")
y = Console.ReadLine()
If board(x, y) = "x" Then
Console.WriteLine("you win!")
End If
Next
5
Example 2:
Dim matrix(4, 4) As Integer
Dim row, column As Integer
For row = 1 To 4
For colum = 1 To 4
Console.Write(" Input value at row: " & row & " and column " & column)
matrix(row, column) = Console.ReadLine()
Next
Next
Console.WriteLine()
For row = 1 To 4
For colum = 1 To 4
Console.Write(matrix(row, column)
matrix(row, column) = Console.ReadLine()
Next
Console.WriteLine()
Next
(a) Ques: Declare an array to make a small checkers board of type char, 3 squares by 3
squares
Ans:
Pseudocode VB.NET
DECLARE checkboard: ARRAY[1:3,1:3] OF CHAR Dim checkBoard(3,3) as char
(b) Create a chequered pattern using b for black and w for white
Pseudocode VB.NET
checkboard[1, 1] ← "b" checkBoard(1, 1) = "b"
checkboard[1, 2] ← "w" checkBoard(1, 2) = "w"
checkboard[1, 3] ← "b" checkBoard(1, 3) = "b"
checkboard[2,1] ← "w" checkBoard(2, 1) = "w"
checkBoard(2, 2) = "b"
checkboard[2, 2] ← "b" checkBoard(2, 3) = "w"
checkboard[2, 3] ← "w"
checkBoard(3, 1) = "b"
checkboard[3, 1] ← "b"
checkBoard(3, 2) = "w"
checkboard[3,2] ← "w" checkBoard(3, 3) = "b"
checkboard[3,3] ← "b"
6
(c) Write an algorithm to display this board (HINT: you need loops)
Pseudocode VB.NET
DECLARE checkboard: ARRAY[1:3,1:3] OF CHAR DIM checkBoard(3,3) AS char
DECLARE x,y: INTEGER DIM x, y AS INTEGER
FOR x ←1 TO 3 FOR x = 1 TO 3
FOR y ←1 TO 3 FOR y = 1 TO 3
PRINT checkBoard(x,y) console.write(checkBoard(x,y))
NEXT NEXT
NEXT console.writeline()
NEXT
Exercises on 2D Array
1. Write a declaration for array Mark in VB.
1 2 3 4 5 6 7
1 45 89 0 80 43 56 52
2 78 23 89 12 78 26 0
3 15 46 25 65 85 76 69
4 89 0 63 45 96 73 82
5 74 96 78 89 45 63 54
(a) Write a declaration for array Mark in VB
(b) What value is stored at Mark[4,5]?
(c) Write code to output all the values in the array Mark
(d) Write code to output all values in row 2
(e) Write code to output all marks below 40
(f) Write code to find the largest mark stored in the array
(g) Write code to calculate the total for each student
(h) Write code to calculate the average of all marks
(i) What output will the following code produce
FOR row ←1 TO 3
FOR column ←2 TO 4
OUTPUT Mark [row, column]
NEXT column
NEXT row
7
8