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.

**A matrix **is a rectangular table made up of elements of the same type (numbers, strings, etc.)< /div>

Such data in Pascal 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 ordinary arrays (lists), in order to work with matrices, you need to learn how to create, enter, process and display them.

##### Matrix creation

###### Task

`Create in memory a matrix of the given size filled with zeros.`
To create a matrix correctly, you need to force the compiler to create all the strings in memory as different objects.

To do this, you must first create an empty array, then specify the number of rows in the matrix, and then specify the number of elements in each row using the

`setLength()`

method in a loop:

N := 3
M := 2
setLength(A, N);
for i := 0 to N - 1 do
setLength(A[i], M);
###

##### Filling a matrix with arbitrary values

After creating a matrix, you can fill it with arbitrary values. Since each element has two indices, it is necessary to use nested loops
for i := 0 to N do begin
for j := 0 to M do begin
A[i][j] := ...
end;
end;
##### Displaying the matrix

Two nested loops are usually used to process and display the list. The first cycle is by the line number, the second cycle is by the elements inside the line.

In order to display the matrix line by line, separating the numbers with spaces within one line, you need to write the following fragment:

for i := 0 to length(A) - 1 do begin // length(A) - returns the number of rows in matrix A
for j := 0 to length(A[i]) - 1 do // length(A[i]) - returns the number of elements in string i
write(a[i][j], ' ');
writeln(); // do a newline after the line is displayed
end;
The same, but cycles not by index, but by list values (the for loop can iterate over all elements in the list (array), string):

for row in a do begin // iterate over all rows of matrix a
for elem in row do // loop through all elements in row
write(elem, ' ');
writeln();
end;