8.5 Scalar and Dot Product Matrix Multiplication 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.
There are a number of multiplication types for matrices. This tutorial (lesson) deals only with what is know as scalar multiplication and dot product.
Scalar Multiplication of a Matrix
Mathematics
The following diagram shows the multiplication of a scalar and a matrix.
Here, the scalar is 2 and the matrix is a 3-by-2 matrix. A matrix of any order is applicable. The scalar multiplies each element in the matrix to give a corresponding matrix of same order. That is:
Row 1/Column 1: 2 x 3 = 6
Row 1/Column 2: 2 x -1 = -2
Row 2/Column 1: 2 x 0 = 0
Row 2/Column 2: 2 x 4 = 8
Row 3/Column 1: 2 x -2 = -4
Row 3/Column 2: 2 x 1 = 2
Program for Multiplying a Scalar and a Matrix
The following program illustrates this multiplication (read through the code and comments):
#include <stdio.h>
void scalarMultiplication (int mat[][2], int scalar, int n, int m) {
int matR[n][m];
for (int i=0; i < n; i++) { //visit rows
for (int j=0; j < m; j++) { //then column cells
matR[i][j] = scalar * mat[i][j]; //multiplication here
printf("%i, ", matR[i][j]);
}
printf("\n");
}
}
int main() {
int mat[3][2];
mat[0][0] = 3; mat[0][1] = -1;
mat[1][0] = 0; mat[1][1] = 4;
mat[2][0] = -2; mat[2][1] = 1;
int scalar=2, n=3, m=2;
scalarMultiplication(mat, scalar, n, m);
return 0;
}
The output is:
6, -2,
0, 8,
-4, 2,
as expected.
The time complexity is actually O(2 x n x m), where 'n x m' refers to the time to iterate (multiply) over all the elements in the matrix (2D array) in the called function. This same amount of time is needed to populate the given matrix with numbers, one-by-one, in the calling function area of the program. And so that makes '2 x n x m'. However, the coefficient (multiplicand of 2) is normally omitted when quoting the time complexity, to give a quoted time complexity of O(n x m), here.
The space complexity is actually O(2 x n x m) for the two matrices: one in the calling function area and a new one (for result) in the called function. However, the coefficient (multiplicand of 2) is normally omitted when quoting the space complexity, to give a quoted space complexity of O(n x m). The O(1) space complexity for the scalar and any other declaration, is ignored.
Multiplying One Matrix by Another
Mathematics
Matrix dot product is a special kind of matrix multiplication. If the multiplicand (first) matrix has the order n-by-m, then the multiplier (second) matrix must have the order, m-by-q. That is, the number of columns in the multiplicand matrix must be the same as the number of rows in the multiplier matrix. The number of rows in the multiplicand matrix must not necessarily be the same as the number of columns in the multiplier matrix. The product matrix has the order, n-by-q.
The following diagram shows such a multiplication of a 3-by-2 and a 2-by-3 matrix.
The multiplication of the above two matrices, means the following:
Row 1/Column 1 in both matrices: (1 x 2) + (-2 x -4) = (2)+(8) = 10 (row1/column1 in Result)
Row 1/Column 2 in both matrices: (1 x -1) + (-2 x 1) = (-1)+(-2) = -3 (row1/column2 in Result)
Row 1/Column 3 in both matrices: (1 x 3) + (-2 x 0) = (3)+(0) = 3 (row1/column3 in Result)
Row 2/Column 1 in both matrices: (3 x 2) + (4 x -4) = (6)+(-16) = -10 (row2/column1 in Result)
Row 2/Column 2 in both matrices: (3 x -1) + (4 x 1) = (-3)+(4) = 1 (row2/column2 in Result)
Row 2/Column 3 in both matrices: (3 x 3) + (4 x 0) = (9)+(0) = 9 (row2/column3 in Result)
Row 3/Column 1 in both matrices: (-1 x 2) + (2 x -4) = (-2)+(-8) = -10 (row3/column1 in Result)
Row 3/Column 2 in both matrices: (-1 x -1) + (2 x 1) = (1)+(2) = 3 (row3/column2 in Result)
Row 3/Column 3 in both matrices: (-1 x 3) + (2 x 0) = (-3)+(0) = -3 (row3/column3 in Result)
Program to Multiply One Matrix by Another
The dot product program is as follows (read through the code and comments):
#include <stdio.h>
void dotProduct(int mat1[][2], int mat2[][3], int n, int m, int q) {
int matR[m][q];
// Loop through each row of mat1
for (int i=0; i < n; i++) {
//Loop through each column of mat2
for (int j=0; j < q; j++) {
// Compute the dot product of
// row mat1[i] and column mat2[][j]
matR[i][j] = 0;
for (int k = 0; k < m; k++) {
matR[i][j] = matR[i][j] + (mat1[i][k] * mat2[k][j]);
}
printf("%i, ", matR[i][j]);
}
printf("\n");
}
}
int main() {
int mat1[3][2];
mat1[0][0] = 1; mat1[0][1] = -2;
mat1[1][0] = 3; mat1[1][1] = 4;
mat1[2][0] = -1; mat1[2][1] = 2;
int mat2[2][3];
mat2[0][0] = 2; mat2[0][1] = -1; mat2[0][2] = 3;
mat2[1][0] = -4; mat2[1][1] = 1; mat2[1][2] = 0;
int n=3, m=2, q=3;
dotProduct(mat1, mat2, n, m, q);
return 0;
}
The output is:
10, -3, 3,
-10, 1, 9,
-10, 3, -3,
as expected.
The time complexity for the dotProduct() function is about O(1½ nxm). However, the coefficient (multiplicand of 1½) is normally omitted when quoting the time complexity, to give a quoted time complexity of O(n x m), here. If the time to assign values to the given matrices are taken into consideration, then the time complexity would actually be O(2½ nxm). However, the coefficient (multiplicand of 2½) is normally omitted when quoting the time complexity, to give a quoted time complexity of O(n x m), for the whole program.
The space complexity is actually O(3 x n x m) for the three matrices: two in the calling function area and a new one (for result) in the called function. However, the coefficient (multiplicand of 3) is normally omitted when quoting the space complexity, to give a quoted space complexity of O(n x m). The O(1) space complexities for the matrix declarations are ignored.
Thanks for reading.
Related Links
More Related LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.