Notes On Two Dimensional Array
Notes On 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
…………..
NEXT Column
NEXT Row
Identifier Explanation
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.
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
Rows Columns
s s
VB
Pseudocode VB
2
Initialising a 2D (2 Dimensional) array
Pseudocode
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
NEXT MonthNumber
NEXT SalesPersonNumber
VB
FOR SalesPersonNumber =1 TO 6
FOR MonthNumber = 1 TO 12
NEXT
NEXT
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
NEXT MonthNumber
NEXT SalesPersonNumber
3
VB
FOR SalesPersonNumber = 1 TO 6
FOR MonthNumber = 1 TO 12
NEXT
NEXT
FOR SalesPersonNumber ← 1 TO 6
FOR MonthNumber ← 1 TO 12
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)
5
Example 2:
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
(b) Create a chequered pattern using b for black and w for white
Pseudocode VB.NET
6
(c) Write an algorithm to display this board (HINT: you need loops)
Pseudocode VB.NET
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 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
7
8