+ 44
Can someone explain to me the algorithm for solving this quiz in C#?🤔😀
var size = 10; var matrix = new int[size, size]; for (int row = 0; row < size; row++) { for (int col = 0; col < row; col++) { matrix [row, col] = 1; } } Console.Write(matrix[3, 7]); Output: 0.
6 odpowiedzi
+ 18
It's because when the row is 3, the column can't be more than 2 (because the limit of the col is row). So when the element at index 3 and 7 is not set by default is 0. Hope it helps you 😉
+ 20
Thanks TheWh¡teCat for the answer😀👍👍
+ 8
You are welcome, Sharof Gafurov🇺🇿 😉👍
+ 7
I was confused trying to evaluate it inside my brain. Its seems like output should be 1 until you realise that nested loop is col < row.
Tricky challenge 😉👍
+ 2
If output matrix[10 , 10]
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
2 1 1 0 0 0 0 0 0 0 0 0
3 1 1 1 0 0 0 0 0 0 0 0
4 1 1 1 1 0 0 0 0 0 0 0
5 1 1 1 1 1 0 0 0 0 0 0
6 1 1 1 1 1 1 0 0 0 0 0
7 1 1 1 1 1 1 1 0 0 0 0
8 1 1 1 1 1 1 1 1 0 0 0
9 1 1 1 1 1 1 1 1 1 0 0
10 1 1 1 1 1 1 1 1 1 1 0
matrix[3 , 7] = 0;
😁😁😁😁😁😁😁😁😁😁
- 2
Example of Ruby