Matrix multiplication in C is a technique of producing a single matrix from two matrices by multiplying them together.
Constraint: For Multiplication of two matrices, the columns of the first matrix must be equal to the rows of the second matrix.
Suppose the first matrix is of the order of [m ,n], so the second matrix should be of the order of [n ,p], else the two matrices can’t be multiplied. The new matrix produced in the above case would be of the order of [m,p].
Examples:
If the first matrix is of order [1,3] and second is of order [3,5], the two matrices can be multiplied together and the new matrix will be of order [1,5]. However, if the first matrix is of order [1,3] and the second is of order [1,5], they cannot be multiplied together.
Example 1:
\( \begin{bmatrix}1 & 3\\
4 & 2
\end{bmatrix}\) * \( \begin{bmatrix}
1 & 2\\
2 & 1
\end{bmatrix}\) = \( \begin{bmatrix}
1*1 + 3*2 & 1*2 + 3*1\\
4*1 + 2*2 & 4*2 + 2*1
\end{bmatrix}\) = \( \begin{bmatrix}
7 & 5\\
8 & 10
\end{bmatrix}\)
Example 2:
\( \begin{bmatrix}1 & 4 & 2\\
3 & 3 & 2
\end{bmatrix}\) * \( \begin{bmatrix}
1\\
2\\
3
\end{bmatrix}\) = \( \begin{bmatrix}
1*1 + 4*2 + 2*3\\
3*1 + 3*2 + 2*3
\end{bmatrix}\) = \( \begin{bmatrix}
15\\
15
\end{bmatrix}\)
Write a C program for multiplication of two matrices.
There are Several ways to perform matrix multiplication. Let us look at two of the approaches:
- Matrix Multiplication in C using Iterative Approach
- Matrix Multiplication in C using Recursive Approach
The idea is to find all the elements of the multiplied matrix by iterating over the row of the first matrix and column of the second matrix. For example, for finding the element at [0,0], we will multiply the first row elements in the first matrix to the corresponding first column matrix in the second matrix.
Here is the source code of the C program to perform matrix multiplication. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to perform matrix multiplication using iterative
*/
#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
return 0;
}
1. Input the size of both matrices
2. If matrix can’t be multiplied print that they can’t be multiplied
3. Else, take input of elements of both matrices
4. Declare a resultant matrix
5. Fill the matrix using formula mul[i][j]+=m1[i][k]*m2[k][j].
6. Print the multiplied matrix.
Example:
Given, first Matrix = \(
\begin{bmatrix}
1 & 3\\
5 & 6
\end{bmatrix}\), second matrix = \(
\begin{bmatrix}
2 & 1\\
4 & 4
\end{bmatrix}\)
Multiplied Matrix = \(
\begin{bmatrix}
1 & 3\\
5 & 6
\end{bmatrix}\) * \(
\begin{bmatrix}
2 & 1\\
4 & 4
\end{bmatrix}\) = \(
\begin{bmatrix}
1*2 + 3*4 & 1*1 + 3*4\\
5*2 + 6*4 & 5*1 + 6*4
\end{bmatrix}\) = \(
\begin{bmatrix}
14 & 13\\
34 & 29
\end{bmatrix}\)
Time Complexity: O(r1*c1*c2)
While finding elements of multiplied matrix, we use two loops for iterating over all elements of matrix and one loop to find the elements, so O(r1*c1*c2).
Space Complexity: O(r1*c1 + r2*c2 + r1*c2)
Since 3 matrices are used, so complexity is O(r1*c1 + r2*c2 + r1*c2).
Testcase 1: In this case, the columns of the first matrix must be equal to the rows of the second matrix.
Enter number of rows for First Matrix: 2 Enter number of columns for First Matrix: 2 Enter number of rows for Second Matrix: 2 Enter number of columns for Second Matrix: 2 Enter first matrix elements 1 3 5 6 Enter Second matrix elements 2 1 4 4 Multiplied matrix 14 13 34 29
Testcase 2: In this case, the columns of the first matrix are not equal to the rows of the second matrix.
Enter number of rows for First Matrix: 3 Enter number of columns for First Matrix: 4 Enter number of rows for Second Matrix: 1 Enter number of columns for Second Matrix: 2 Matrices Can't be multiplied together
In the recursive approach, we will use function calls to find the elements of the multiplied matrix.
Here is the source code of the C program to perform matrix multiplication using Recursive approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to perform matrix multiplication using recursive
*/
#include<stdio.h>
#define m 100
void multiply (int r1, int c1, int m1[m][m], int r2, int c2, int m2[m][m], int mul[m][m])
{
//Declaring Static because we want them to same in all function calls
static int i = 0, j = 0, k = 0;
if (i >= r1)
{
return;
}
else if (i < r1)
{
if (j < c2)
{
if (k < c1)
{
mul[i][j] += m1[i][k] * m2[k][j];
k++;
//Function call for multiplying all row element with column element
multiply(r1, c1, m1, r2, c2, m2, mul);
}
k = 0;
j++;
//Function call for all elements of row of multiplied matrix
multiply(r1, c1, m1, r2, c2, m2, mul);
}
j = 0;
i++;
//Function call for all rows
multiply(r1, c1, m1, r2, c2, m2, mul);
}
}
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
printf("Multiplication Not Possible");
else
{
int m1[m][m], m2[m][m], mul[m][m] = {0};
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
multiply(r1,c1,m1,r2,c2,m2,mul);
printf("Multiplied Matrix:\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
}
return 0;
}
1. Input the size of both matrices
2. If matrix can’t be multiplied print that they can’t be multiplied
3. Else, Take input of elements of both matrices
4. Declare function multiply for matrix multiplication
5. In multiply function,there are 3 function calls
- First, multiply all row elements with all column elements. (basic step of matrix multiplication)
- Second, repeat step one for all row elements.
- Third, repeatsecond step for all rows.
6. Print Multiplied Matrix.
Time Complexity: O(r1*c1*c2)
The first function call will make c1 function calls to find corresponding elements, the second function call will make c2 function calls to find all elements in a row, and the third function call will make r1 function calls to find all rows.
Space Complexity: O(r1*c1 + r2*c2 + r1*c2)
Since 3 matrices are used, so complexity is O(r1*c1 + r2*c2 + r1*c2).
Enter number of rows for First Matrix: 2 Enter number of columns for First Matrix: 2 Enter number of rows for Second Matrix: 2 Enter number of columns for Second Matrix: 2 Enter first matrix elements 1 3 5 6 Enter Second matrix elements 2 1 4 4 Multiplied matrix 14 13 34 29
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
If you find any mistake above, kindly email to [email protected]- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos