C Program: Matrix Multiplications

In this tutorial, we will write a program to perform simple matrix multiplication in C.

The product of two matrices $A = [a_{ij}]$ and $B = [b_{ij}]$ is said to be defined in the order $AB$ if and only if the number of columns of matrix $A$ is equal to the number of rows of matrix $B$. The two matrices are then said to be conformable for multiplication.

If you multiply an $m \times p$ matrix $A = [a_{ij}]$ with an $p \times n$ matrix $B = [b_{ij}]$, the resulting product matrix will be an $m \times n$ matrix $C = [c_{ij}]$ where

$$ c_{ij} = a_{i1}b_{1j} + a_{i2}b_{2j} + ... + a_{ip}b_{pj} $$

and the indices $i = (1,2, ... ,m)$ and $j = (1,2, ... ,n)$.

The product of two matrices $A$ and $B$ in the order $AB$ is written expicitly as

$$ AB = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix} $$

We recall that the operaton of matrix multiplication is row to column, so each element of a particular row in the first matrix is multiplied into the corresponding element of the column in the second matrix, which are then added together.

If $AB$ is defined, then

$$ AB = \begin{bmatrix} a_{11}b_{11} + a_{12}b_{21} & a_{11}b_{12} + a_{12}b_{22} \\ a_{21}b_{11} + a_{22}b_{21} & a_{21}b_{12} + a_{22}b_{22} \\ a_{31}b_{11} + a_{32}b_{21} & a_{31}b_{12} + a_{32}b_{22} \end{bmatrix} $$

Here we write a C program to find the product of two matrices $A$ and $B$ based on the above defined operations. We will be performing multiplication of matrices in C using arrays. We create two two-dimesnional arrays A and B to store the elements of the matrices $A$ and $B$. It will be a multiplication of two 2D arrays in C. The if(p != q) condition checks if the number of columns of matrix $A$ equals the number of rows of matrix $B$. The program proceeds only if they are equal; else, the program terminates with the call of the exit() function. The <stdlib.h> header file is included to make use of the exit() function. The resulting product matrix is assigned to array C.

				
				#include <stdio.h>
				#include <stdlib.h>
				int main() {
				  int A[10][10], B[10][10], C[10][10];
				  unsigned short m, p, q, n, i, j, k;
				  printf("Rows (Matrix A): ");
				  scanf("%hu", &m);
				  printf("Columns (Matrix A): ");
				  scanf("%hu", &p);
				  printf("Rows (Matrix B): ");
				  scanf("%hu", &q);
				  if(p != q) {
				  	printf("THE TWO MATRICES ARE NOT CONFORMABLE.\n");
				  	exit(0);
				  }
				  printf("Columns (Matrix B): ");
				  scanf("%hu", &n);
				  // ELEMENTS OF MATRIX A
				  for(i=0; i < m; i++) {    
				    for(j=0; j < p; j++) { 
				      printf("A[%hu,%hu]: ", i, j);    
				      scanf("%d", &A[i][j]);    
				    }    
				  }  
				  // ELEMENTS OF MATRIX B
				  for(j=0; j < q; j++) {    
				    for(k=0; k < n; k++) { 
				      printf("B[%hu,%hu]: ", j, k);    
				      scanf("%d", &B[j][k]);    
				    }    
				  }  
				  // DISPLAY MATRIX A
				  printf("Matrix A\n");
				  for(i = 0; i < m; i++) {
				    for(j = 0; j < p; j++) {
				      printf("%d ", A[i][j]);
				    }
				    printf("\n");
				  }
				  // DISPLAY MATRIX B
				  printf("Matrix B\n");
				  for(j = 0; j < q; j++) {
				    for(k = 0; k < n; k++) {
				      printf("%d ", B[j][k]);
				    }
				    printf("\n");
				  }
				  // MULTIPLICATION
				  for(i=0; i < m; i++) {    
				    for(j=0; j < n; j++) {    
				      C[i][j] = 0; 
				      for(k=0; k < p; k++) { // k < q WILL ALSO WORK 
				        C[i][j] += A[i][k]*B[k][j];    
				      }    
				    }    
				  }
				  // DISPLAY MATRIX AB
				  printf("Product Matrix AB\n");
				  for(i = 0; i < m; i++) {
				    for(k = 0; k < n; k++) {
				      printf("%d ", C[i][k]);
				    }
				    printf("\n");
				  }
				  return 0;
				}
				
			

We pick an example from a Schaum's Outline Series book Theory and Problems of Matrices by Frank Aryes, Jr1. $A$ is a $3 \times 3$ matrix

$$ A = \begin{bmatrix} 2 & 1 & 0 \\ 3 & 2 & 0 \\ 1 & 0 & 1 \end{bmatrix} $$

and $B$ is a $3 \times 4$ matrix

$$ B = \begin{bmatrix} 1 & 1 & 1 & 0\\ 2 & 1 & 1 & 0\\ 2 & 3 & 1 & 2 \end{bmatrix} $$

Since the number of columns of matrix $A$ is equal to the number of rows of matrix $B$, the product $AB$ is defined and the two matrices are conformable for multiplication in that order. The resulting product matrix will be a $3 \times 4$ matrix.

We will now run the above C program to find the product of these two matrices, which is

$$ \begin{bmatrix} 2 & 1 & 0 \\ 3 & 2 & 0 \\ 1 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 1 & 1 & 0\\ 2 & 1 & 1 & 0\\ 2 & 3 & 1 & 2 \end{bmatrix} $$

				
					$ ./a.out		
				
			

We input the row and column dimensions of both the matrices and the elements.

The program finally prints out the resulting product matrix as

$$ \begin{bmatrix} 4 & 3 & 3 & 0\\ 7 & 5 & 5 & 0\\ 3 & 4 & 2 & 2 \end{bmatrix} $$

Notes