Broad Network


8.1 Matrix and its Coding in C

8. Basic Matrix Programming in C

Full Course on Data Structures and Algorithms in C

By: Chrysanthus Date Published: 3 Feb 2026

The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.

A matrix is a rectangular arrangement of numbers. This rectangular pattern is arranged horizontally by rows and vertically by columns. The following diagram shows a generalized m-by-n (i.e. m x n) matrix, where m is the number of rows and n is the number of columns.

Row counting begins from the top and going downwards. Column counting begins from the left, and going rightwards. In the diagram, 'a' represents a number. The first subscript is the row number and the second subscript is the column number.

Example of a 2-by-3 Matrix

The following diagram shows an example of a '2 x 3' matrix:

There are 2 rows and 3 columns that form a rectangle.

Square Matrix

A square shape is a rectangle but a rectangle is not necessarily a square. The following diagram is an example of a 3-by-3 square matrix.

There are 3 rows and 3 columns that form a square. In a square matrix the number of rows are equal to the number of columns.

The order of a Matrix

The size or order of a matrix is the number of rows and columns of the matrix, given in general terms as n-by-m (i.e. n x m), where n is the number of rows and m is the number of columns. In mathematics it is written as m-by-n for number of rows and number of columns. However, when dealing with time and space complexity, it is written as n-by-m, since for one-dimensional array, n is normally used for number of rows. Now that there are rows and columns, it is better to extend to the columns with m. n-by-m (n x m) is the convention used in this full course.

Coding a Matrix

In programming, a matrix is a two-dimensional array. Rows start from the top and come down, and columns start from the left and go right. However, a programming array employs zero based indexing, while the mathematics matrix employs one based indexing.

Example Coding for a Matrix

The following program illustrates how a '2 x 3' matrix can be coded:

#include <stdio.h>

int main()
{
    int matrix2b3[2][3] = {{2, -5, 8}, {-3, 1, -4}};
    
    for (int i=0; i < 2; i++) {
        for (int j=0; j < 3; j++) {
            printf("%i ", matrix2b3[i][j]);
        }
        printf("\n");
    }
    return 0;
}

The output is:

    2 -5 8 
    -3 1 -4 

The time complexity is actually O(2 x n x m). The first n x m is for the declaration of the matrix with all values assigned. The second n x m is for the printing nesting for-loops. However, the time complexity is quoted as O(n x m), with the coefficient (multiplicand of 2) omitted. The space complexity is O(n x m) for the matrix (2D array).

A 2D array is accessed in a very similar way to which a 1D array is accessed. In the square brackets, the first whole number is for the row and the second whole number is for the column.

In the declaration of the matrix above, the different row arrays are typed on the right-hand-side of the statement; that automatically assigns the column values as well.

In the following program, the matrix is declared first, and the row values (and automatically column values) are assigned shortly after.

#include <stdio.h>

int main() 
{
    int matrix2b3[2][3];
    matrix2b3[0][0] = 2; matrix2b3[0][1] = -5; matrix2b3[0][2] = 8; 
    matrix2b3[1][0] = -3; matrix2b3[1][1] = 1; matrix2b3[1][2] = -4; 

    for (int i=0; i<2; i++) {
        for (int j=0; j<3; j++) {
            printf("%i ", matrix2b3[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

The output is:

    2 -5 8 
    -3 1 -4 

same as above.

The time complexity is actually O(2 x n x m). It is for assigning n rows times m column values, one-by-one, since the declaration of the matrix does not have assigning values. It is also for the printing nesting for-loops. However, the time complexity is quoted as O(n x m), with the coefficient (multiplicand of 2) omitted. Its O(1) time is ignored. The space complexity is O(n x m) for the matrix (2D array). The O(1) space for the matrix declaration is ignored.

Thanks for reading.





Related Links

More Related Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.