How to Create a circular matrix table in
C# .NET
Here will discuss how to create a circular matrix in the C#. matrix is a two-dimensional
array of numeric values, which contain any type of data.
below is the 3×3 matrix:
3 |
23 |
44 |
65 |
32 |
43 |
87 |
6 |
7 |
Matrices contain columns and rows and start with a 0 index. below are the coordinate of reading a matrix
0,0 |
0,1 |
0,2 |
|
1,0 |
1,1 |
1,2 |
|
2,0 |
2,1 |
2,2 |
|
|
|
The matrix starts with (0, 0) coordinates, generally 0 or 1,. below is an example of a 3×3 matrix:
1 |
2 |
3 |
|
11 |
12 |
13 |
|
21 |
22 |
23 |
|
|
|
So we start with 1 and then till row 3. Then we continue to 23 (2, 2 coordinates), followed by the bottom row, and then again we do the same.
the top row is 1, 2, and 3 but the second row is 11, 12, and 13 which doesn’t reveal anything to me. Looking at the columns is even less
promising: 1, 12, 11, 10, and then 2, 13, 16, and 9. Frankly, I don’t see
anything that catches the eye, no symmetries, no patterns that can be modeled
in a loop, etc.
Therefore a more viable solution is to follow the index as it’s
incremented and in the outer circle. We then do the same in the inner circle
and continue inwards for any remaining circles.
So in the first iteration, we want to fill the matrix as
below:
In the second iteration, we follow the same route with the remaining, smaller inner matrix: |
|
This iteration will be an outer one in the code. We’ll also need
to iterate through the various sections:
§ top row rightwards
§ right column downwards
§ bottom row leftwards
§ left column upwards
below is the code for the same
here we are returning a temporary jagged array and then filling them with some empty values. We need to see that the array contents are copied to the list and then returned from the given function.
We’ll need many counters. We need to know where we start and end each row and the column in the main outer loop. need to Remember, the main outer loop serves to identify the dimensions of the array.
1 |
1 |
1 |
|
1 |
2 |
2 |
|
1 |
2 |
2 |
|
|
|
|
|
In a 3×3 matrix, we have 2 circles. They are denoted by purple
1’s and reds 2’s in the above example. A 3×3 matrix will also have 2 circles but
the inner circle has only one element, the one in the very middle of the
matrix. A 2×2 matric has only a single circle.
The counter starts at 1 in row 0 and column 0, those are 3
variables that we can identify right there. Those are valid for the first
circle. When we’re done with the first circle then the counter will have
whatever value it has got up to in the iteration, whereas the start row and
start column will be 1. (1, 1) is the starting point for the inner circle of a
4×4 matrix – and for any other matrix of any size for that matter.
We also need to see how far out we’re allowed to go in each circular
iteration. In the first iteration, we’ll start at (0, 0) and the farthest we
reach is at (3, 3), i.e. the bottom right-hand cell of the matrix. In the
second circle, we start at (1, 1) and the max reach will be at (2, 2) otherwise
we overwrite some of the values we have already calculated. It sounds like it’s
not enough to know where we start, i.e. start column and start row, but where
we end as well, i.e. the end column and end row.
If you refer back to the table with the arrows then we’ll need
to set these row and column pointers as follows:
§ start row: 0
§ end row: n – 1, i.e. 3 in a 4×4
matrix to comply with the 0-based array indexing
§ start column: 0
§ end column: n – 1, same as for end
row
We start filling up the top row, i.e. from the start row to the
end column, (0, 0) to (0, 3) in a 4×4 matrix. When we’re done then we increment
the start row to 1. At this point, we’re filling up the right-hand column from
(1, 3) to (3, 3). Then we decrement the end column and continue filling up the
bottom row from (3, 2) to (3, 0). Finally, we move up, i.e. decrement the end
row and fill in the cells from (2, 0) to (1, 0). Recall that we incremented the
start row to 1 so we don’t return to (0, 0) when filling up the left-hand
column.
At this point start row and start column are 1, end row and end
column are 2, we’re ready for the inner circle where the same steps are taken.
So we’ll need the following:
§ the main loop for each circle in the
matrix
§ inner loops for each section of the
circle
§ top row
§ right column
§ bottom row
§ left column
Also, the bottom row and left column are iterated backward, i.e.
we need a for-loop where the iterator integer decreases to make sure we’re
moving in the right direction.
We break the main outer loop when we have reached the end column
and the end row, i.e. the start row and the start columns have converged to the
end equivalents.
This is a lot of talking that’s difficult to visualize so let’s see a partial implementation with only the counter values and the various loops:
Those loops pave the way for us to keep the index right, i.e. that we’re in the correct cell all the time. The while loop represents the circles in the matrix and is broken when the start and end indexes of the rows and columns have converged, i.e. we have reached the final cell in the matrix.
The 4 for-loops represent the 4 directions we’re taking, as
outlined above. Also, notice the decreasing direction of the second two loops
that we also hinted at above.
Now that we have the correct pointers we can increase the counter variable within each for-loop, that’s simple:
We have the pointers to the cell coordinates and the integer values we want to put in them. Now we need to use those to fill in the jagged array. This is where it’s absolutely necessary to use arrays and not lists because we fill the arrays the same way as we’re traversing the matrix. Each array within the temporary array represents a row and when we’re done with the first for-loop we’ll have the following jagged array:
1 |
2 |
3 |
|
x |
x |
x |
|
x |
x |
x |
|
|
|
|
|
Then we move on and fill in the rightmost column:
1 |
2 |
3 |
|
x |
x |
x |
|
x |
x |
x |
|
|
|
|
|
So we need to access the last spots of arrays 2, 3, and 4 of the
2-dimensional array. We could not do the same with a list, there’s no
equivalent accessor to a list where we can set the value at position n, we can
only add new values to the end of the list. So this is the main reason why it’s
better to go for an array.
Next up we’ll fill up the bottom row:
1 |
2 |
3 |
|
x |
x |
x |
|
x |
x |
x |
|
|
|
|
|
…and finally the left-hand column:
1 |
2 |
3 |
|
12 |
x |
x |
|
11 |
x |
x |
|
|
|
|
|
We then do the same in the “inner” matrix:
1 |
2 |
3 |
|
12 |
13 |
14 |
|
11 |
16 |
15 |
|
|
|
|
|
Now we need to select the correct indexes for the jagged array
in the 4 for-loops.
In the first loop, we move from left to right, i.e. from (0, 0)
to (0, 3). So the row is constant, it will be equal to startRow. The column increases from 0
to endColumn. Hence we’ll use the following
accessors:
1 |
temporaryArray[startRow][i] |
In the second for loop, we move from (1, 3) to (3, 3). The
column is equal to the end column and the row moves up from startRow to and including endRow. Recall that we incremented startRow by 1 after the first loop.
Therefore we won’t accidentally overwrite cells (0, 3). Here’s the accessor in
the second loop:
1 |
temporaryArray[i][endColumn] |
In the third loop, we move to the left from (3, 2) to (3, 0) and
in the fourth loop upwards from (2, 0) to (1, 0).
Here’s the full solution:
I think that was quite a challenging problem. Make sure you study it before your next job interview so that you can quickly present your solution and move on to the next problem.
Reference:- https://dotnetcodr.com/2020/10/21/how-to-build-a-circular-matrix-with-c- net/
-------------------------------------------------------------------------
-------------------------------------------------------------------------
EmoticonEmoticon