Skip to content

Commit adaf20a

Browse files
author
Clément
committed
Adding some exercises on 2d arrays.
1 parent f22c278 commit adaf20a

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

source/code/snippets/2darrays.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
// Example array1
8+
int[,] matrix =
9+
{
10+
{ 1, 2, 3 },
11+
{ 4, 5, 6 },
12+
{ 7, 8, 9 },
13+
{ 10, 11, 12 },
14+
};
15+
// End of example array1
16+
Display(matrix);
17+
Rotate(ref matrix);
18+
Display(matrix);
19+
}
20+
21+
// Method to display 2-d arrays
22+
static void Display(int[,] matP)
23+
{
24+
for (int row = 0; row < matP.GetLength(0); row++)
25+
{
26+
for (int col = 0; col < matP.GetLength(1); col++)
27+
{
28+
Console.Write(
29+
String.Format("|{0,4} ", matP[row, col])
30+
);
31+
}
32+
Console.WriteLine(" |");
33+
}
34+
}
35+
36+
// End of method to display 2-d arrays
37+
38+
// Method to rotate an array 90° clockwise
39+
static void Rotate(ref int[,] matP)
40+
{
41+
int[,] tmp = new int[
42+
matP.GetLength(1),
43+
matP.GetLength(0)
44+
];
45+
for (int row = 0; row < tmp.GetLength(0); row++)
46+
{
47+
for (int col = 0; col < tmp.GetLength(1); col++)
48+
{
49+
tmp[row, col] = matP[
50+
tmp.GetLength(1) - col - 1,
51+
row
52+
];
53+
}
54+
}
55+
matP = tmp;
56+
}
57+
// End of method to rotate an array 90° clockwise
58+
}

source/solutions/collections/2darrays.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tags:
6565
| 40 | 50 | 60 |
6666
| 70 | 80 | 90 |
6767

68-
<details>
68+
<details>
6969
<summary>Solution</summary>
7070

7171
```
@@ -112,6 +112,14 @@ tags:
112112
By using the `Length` field: `temp.Length` is the number of elements in the `temp` array.
113113
We can also compute the product of `temp.GetLength(0)` and `temp.GetLength(1)`.
114114
</details>
115+
116+
#. Write a `Display` static method that takes as an argument a 2-dimensional array and displays it at the screen.
117+
118+
<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
121+
```
122+
</details>
115123

116124
#. Write a program that display "Every row contains its own index" if the 2-dimensional rectangular array of `int` `matrix` is such that its first row contains the value 0, its second row contains the value 1, etc.
117125

@@ -172,6 +180,26 @@ tags:
172180
```
173181
</details>
174182

183+
#. Write a program that "rotate" a 2-dimensional array 90° clockwise. For example, the array
184+
185+
```
186+
!include`snippetStart="// Example array1", snippetEnd="// End of example array1"` code/snippets/2darrays.cs
187+
```
188+
189+
would become
190+
191+
```text
192+
| 10 | 7 | 4 | 1 |
193+
| 11 | 8 | 5 | 2 |
194+
| 12 | 9 | 6 | 3 |
195+
```
196+
197+
<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
200+
```
201+
</details>
202+
175203
## Problem: Toward a Crossword Puzzle Solver
176204

177205
The goal of this problem is to work toward the creation of a program that solve crossword puzzles.
@@ -191,9 +219,9 @@ Test your program thoroughly, possibly bundling it in a `static` class to ease t
191219
<details>
192220
<summary>Solution</summary>
193221
A possible implementation, as a static class, is as follows:
194-
195222
```{download="./code/projects/CrossWord.zip"}
196-
!include code/projects/CrossWord/CrossWord/CrossWord.cs
197-
```</details>
223+
!include code/projects/CrossWord/CrossWord/Crossword.cs
224+
```
198225
</details>
199226

227+

0 commit comments

Comments
 (0)