Skip to content

Commit bc8939c

Browse files
committed
Improving notes on 2D arrays.
1 parent f0de68e commit bc8939c

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

source/code/snippets/2darrays.cs renamed to source/code/projects/2DarrayExercise/2DarrayExercise/Program.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,33 @@ class Program
55
static void Main()
66
{
77
// Example array1
8-
int[,] matrix =
8+
int[,] matrix1 =
99
{
1010
{ 1, 2, 3 },
1111
{ 4, 5, 6 },
1212
{ 7, 8, 9 },
1313
{ 10, 11, 12 },
1414
};
1515
// End of example array1
16-
Display(matrix);
17-
Rotate(ref matrix);
18-
Display(matrix);
16+
Console.WriteLine("Example 1:");
17+
Display(matrix1);
18+
Console.WriteLine("We now rotate the array.");
19+
Rotate(ref matrix1);
20+
Display(matrix1);
21+
22+
// Example array2
23+
int[,] matrix2 =
24+
{
25+
{ 1, 2, 3, 4 },
26+
{ 5, 6, 7, 8 },
27+
{ 9, 10, 11, 12 }
28+
};
29+
// End of example array2
30+
Console.WriteLine("Example 2:");
31+
Display(matrix2);
32+
Console.WriteLine("We now rotate the array.");
33+
Rotate(ref matrix2);
34+
Display(matrix2);
1935
}
2036

2137
// Method to display 2-d arrays
@@ -47,8 +63,10 @@ static void Rotate(ref int[,] matP)
4763
for (int col = 0; col < tmp.GetLength(1); col++)
4864
{
4965
tmp[row, col] = matP[
50-
tmp.GetLength(1) - col - 1,
51-
row
66+
// This would be incorrect:
67+
// tmp.GetLength(0) - col, row
68+
// can you figure out why?
69+
tmp.GetLength(1) - col - 1, row
5270
];
5371
}
5472
}

source/lectures/data/manipulating_rectangular_arrays.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The code for this lecture is available [in this archive](./code/projects/MagicSq
1313

1414
The following code sum the values contained in a 2-dimensional array row per row, and display the result each time before moving on to the next row:
1515

16-
```
16+
```{download="./code/projects/MagicSquare.zip"}
1717
!include`snippetStart="// Summing the values row per row", snippetEnd="As an exercise, you can"` code/projects/MagicSquare/MagicSquare/Program.cs
1818
```
1919

@@ -25,7 +25,7 @@ A [magic square](https://en.wikipedia.org/wiki/Magic_square) is a square matrix
2525

2626
The following is an example of a magic square:
2727

28-
```
28+
```{download="./code/projects/MagicSquare.zip"}
2929
!include`snippetStart="// Examples courtesy of https://en.wikipedia.org/wiki/Magic_square", snippetEnd="// Check by hand that this square is indeed magic!"` code/projects/MagicSquare/MagicSquare/Program.cs
3030
```
3131

@@ -47,7 +47,7 @@ $$2+7+6=15$$
4747

4848
A method to return `true` if the 2d-matrix of `int` passed as an argument is a magic square is as follows:
4949

50-
```
50+
```{download="./code/projects/MagicSquare.zip"}
5151
!include code/projects/MagicSquare/MagicSquare/MagicSquare.cs
5252
```
5353

source/solutions/data/2darrays.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ tags:
116116
#. Write a `Display` static method that takes as an argument a 2-dimensional array and displays it at the screen.
117117

118118
<details>
119-
<summary>Solution</summary>```
120-
!include`snippetStart="// Method to display 2-d arrays", snippetEnd="// End of method to display 2-d arrays"` code/snippets/2darrays.cs
119+
<summary>Solution</summary>```{download="./code/projects/2DarrayExercise.zip"}
120+
!include`snippetStart="// Method to display 2-d arrays", snippetEnd="// End of method to display 2-d arrays"` code/projects/2DarrayExercise/2DarrayExercise/Program.cs
121121
```
122122
</details>
123123

@@ -183,7 +183,7 @@ tags:
183183
#. Write a program that "rotate" a 2-dimensional array 90° clockwise. For example, the array
184184

185185
```
186-
!include`snippetStart="// Example array1", snippetEnd="// End of example array1"` code/snippets/2darrays.cs
186+
!include`snippetStart="// Example array1", snippetEnd="// End of example array1"` code/projects/2DarrayExercise/2DarrayExercise/Program.cs
187187
```
188188

189189
would become
@@ -195,8 +195,8 @@ tags:
195195
```
196196

197197
<details>
198-
<summary>Solution</summary>```
199-
!include`snippetStart="// Method to rotate an array 90° clockwise", snippetEnd="// End of method to rotate an array 90° clockwise"` code/snippets/2darrays.cs
198+
<summary>Solution</summary>```{download="./code/projects/2DarrayExercise.zip"}
199+
!include`snippetStart="// Method to rotate an array 90° clockwise", snippetEnd="// End of method to rotate an array 90° clockwise"` code/projects/2DarrayExercise/2DarrayExercise/Program.cs
200200
```
201201
</details>
202202

0 commit comments

Comments
 (0)