Skip to content

Commit 78af3a3

Browse files
committedMar 5, 2016
Rename variable to matrix
1 parent 52b8557 commit 78af3a3

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed
 
+42-43
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
/*
2-
Two-dimensional array with a fixed number of rows and columns.
3-
This is mostly handy for games that are played on a grid, such as chess.
4-
Performance is always O(1).
2+
Two-dimensional array with a fixed number of rows and columns.
3+
This is mostly handy for games that are played on a grid, such as chess.
4+
Performance is always O(1).
55
*/
66
public struct Array2D<T> {
7-
public let columns: Int
8-
public let rows: Int
9-
private var array: [T]
10-
11-
public init(columns: Int, rows: Int, initialValue: T) {
12-
self.columns = columns
13-
self.rows = rows
14-
array = .init(count: rows*columns, repeatedValue: initialValue)
7+
public let columns: Int
8+
public let rows: Int
9+
private var array: [T]
10+
11+
public init(columns: Int, rows: Int, initialValue: T) {
12+
self.columns = columns
13+
self.rows = rows
14+
array = .init(count: rows*columns, repeatedValue: initialValue)
15+
}
16+
17+
public subscript(column: Int, row: Int) -> T {
18+
get {
19+
return array[row*columns + column]
1520
}
16-
17-
public subscript(column: Int, row: Int) -> T {
18-
get {
19-
return array[row*columns + column]
20-
}
21-
set {
22-
array[row*columns + column] = newValue
23-
}
21+
set {
22+
array[row*columns + column] = newValue
2423
}
25-
24+
}
2625
}
2726

27+
28+
2829
// initialization
29-
var Array2DNumbers = Array2D(columns: 3, rows: 5, initialValue: 0)
30+
var matrix = Array2D(columns: 3, rows: 5, initialValue: 0)
3031

3132
// makes an array of rows * columns elements all filled with zero
32-
print(Array2DNumbers.array)
33+
print(matrix.array)
3334

3435
// setting numbers using subscript [x, y]
35-
Array2DNumbers[0, 0] = 1
36-
Array2DNumbers[1, 0] = 2
36+
matrix[0, 0] = 1
37+
matrix[1, 0] = 2
3738

38-
Array2DNumbers[0, 1] = 3
39-
Array2DNumbers[1, 1] = 4
39+
matrix[0, 1] = 3
40+
matrix[1, 1] = 4
4041

41-
Array2DNumbers[0, 2] = 5
42-
Array2DNumbers[1, 2] = 6
42+
matrix[0, 2] = 5
43+
matrix[1, 2] = 6
4344

44-
Array2DNumbers[0, 3] = 7
45-
Array2DNumbers[1, 3] = 8
46-
Array2DNumbers[2, 3] = 9
45+
matrix[0, 3] = 7
46+
matrix[1, 3] = 8
47+
matrix[2, 3] = 9
4748

4849
// now the numbers are set in the array
49-
print(Array2DNumbers.array)
50+
print(matrix.array)
5051

5152
// print out the 2D array with a reference around the grid
52-
for i in 0..<Array2DNumbers.rows {
53-
print("[", terminator: "");
54-
for j in 0..<Array2DNumbers.columns {
55-
if (j == Array2DNumbers.columns - 1) {
56-
print("\(Array2DNumbers[j, i])", terminator: "");
57-
} else {
58-
print("\(Array2DNumbers[j, i]) ", terminator: "");
59-
}
53+
for i in 0..<matrix.rows {
54+
print("[", terminator: "");
55+
for j in 0..<matrix.columns {
56+
if (j == matrix.columns - 1) {
57+
print("\(matrix[j, i])", terminator: "");
58+
} else {
59+
print("\(matrix[j, i]) ", terminator: "");
6060
}
61-
print("]");
61+
}
62+
print("]");
6263
}
6364

64-
65-

0 commit comments

Comments
 (0)
Please sign in to comment.