Introduction.

Very often, when working with any information, you have to deal with tabular data. Modern programs very often work with such data. The simplest example is programming games on a checkered board: tic-tac-toe, chess, checkers, etc.

In mathematics, such structures are called matrices.

 
Matrix is a rectangular table composed of elements of the same type (numbers, strings, etc.).

Such data in C# can be stored and processed as two-dimensional arrays - "arrays of arrays".
To process the data in the table, it is necessary to remember the state of each cell (cell). Each cell has two numbers: a row number and a column number.
In the matrix, each element has two indices: first the row number is indicated, then the column number. Numbering of rows and columns starts from zero.
For example, the element A[1, 2] is the element located in the second row and third column.

Just like with regular arrays, in order to work with matrices, you need to learn them create, enter, process and display.

Create a matrix.

It is possible to create a in-memory matrix of a given size filled with some initial values.

int[,] array = new int[4, 2]; // 4 rows, 2 columns

 

After creating a matrix, you can fill it with arbitrary values. Since each element has two indexes, it is necessary to use nested loops

for (int i=1; i <= N, i++) { / / N - number of lines
    for (int j=1; j <= N, j++) { // M is the number of columns
        A[i, j] = ...
  }
}
Displaying the matrix.

Two nested loops are usually used to process and display the list. The first loop is on the line number, the second loop is on the elements within the line. To output matrix onto the screen line by line, separating the numbers with spaces within one line, you need to write the following fragment:
 
for (int i=1; i <= N, i++) {
    for (int j=1; j <= N, j++) {
        Console. Write(A[i, j] + " ");
  }
  Console.WriteLine(); // move to a new line
}
   

You can do the same, but use list loops instead of index (foreach). Try to write a subroutine that implements this yourself.

Filling a matrix with values ​​from the keyboard.

Let the program receive a two-dimensional array as input, in the form n strings, each containing m numbers separated by spaces. How to count them? For example, like this:

int[,] array = new int[n,m]; // create an array to populate
for (int i = 0; i <  n; i++)
{
       string A = Console.ReadLine(); // read line
       int[] curr = A.Split(' ').Select(int.Parse).ToArray(); // convert this string to a one-dimensional array curr
       for (int j = 0; j < m; j++)
       {
           array[i, j] = curr[j]; // fill in the string of the array we need with the values ​​from the curr array
       }
}

 

Each element of the matrix has two indexes, so you need to use a nested loop to iterate through all the elements.
Usually a matrix is ​​iterated row by row: the outer loop iterates over the row indices, while the inner loop iterates over the column indices.
But if necessary, you can iterate over the matrix and by columns, then the cycles are reversed.