Problem

1 /12


What is a matrix? Creation, display

Theory Click to read/hide

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.

Problem

Fill in a binary matrix (consisting of only 0s and 1s) in a checkerboard pattern. There should be a null element in the upper left corner.

Input data: the input line contains space-separated matrix dimensions: number of rows and number of columns ( 1 <= M <=< /em> 100 ).

Output: The program should output a binary matrix row by row.

Example.
# Input Output
1 4 5 0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1