Matrix

Matrix is multidimensional array. Basically array containing arrays.

The simplest matrix is the two-dimensional Array. In general it looks something like this:

matrix

Each element is located at certain row and column.

Initializing Matrix

Let's create the matrix from the above picture:

This matrix has 3 rows and 5 columns.

Accessing elements

In order to access element the index of the row and column must be specified.

Let's print the element of the matrix that's located in the 2nd row (index 1) and 4th column (index 3).

access-matrix-element

Output:

Notice that the first index corresponds to the row (1) and the last one to the column (3).

Printing Elements Of A matrix:

Nested loops are suitable for iterating matrix elements.

Output: