Approach
For solving this problem, we will first input the two matrices from the user. Then we will check if the matrix can be multiplied or not by checking that the column of the first matrix should be equal to the row of the second matrix. If that condition satisfies then only the matrix will be multiplied. After that we will simply pass the 3 loops to multiply the two matrices by the formula c[i][j]+=a[i][k]*b[k][j]. In the end, we will print the resultant matrix.
Algorithm of C Programming Matrix Multiplication
Step 1: Initialize the matrices:
- Read matrix AAA with dimensions m×nm \times nm×n.
- Read matrix BBB with dimensions n×pn \times pn×p.
- Initialize matrix CCC with dimensions m×pm \times pm×p to store the result.
Step 2: Matrix Multiplication:
- For each element C[i][j]C[i][j]C[i][j] in matrix CCC:
- Set C[i][j]C[i][j]C[i][j] to 0.
- For each element in the row of AAA and the corresponding element in the column of BBB:
- Multiply A[i][k]A[i][k]A[i][k] and B[k][j]B[k][j]B[k][j], and add the result to C[i][j]C[i][j]C[i][j].
Step 3: Print the resultant matrix CCC.
Pseudocode
for(int i=0;i<row1;i++){
for(int j=0;j<col2;j++){
c[i][j]=0;
for(int k=0;k<col1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
Implementation in Java
// java program to find the matrix multiplication.
import java.util.*;
class matrixmuli
{
public
static void main(String args[])
{
int[][] a = new int[10][10];
int[][] b = new int[10][10];
int[][] c = new int[10][10];
int row1, col1, row2, col2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the rows of first matrix");
row1 = sc.nextInt();
System.out.println("Enter the col of first matrix");
col1 = sc.nextInt();
System.out.println("Enter the rows of second matrix");
row2 = sc.nextInt();
System.out.println("Enter the col of second matrix");
col2 = sc.nextInt();
if (col1 != row2)
{
System.out.println(" -----please enter valid inputs----");
// break;
}
else
{
// taking the elements of first matrix by user
System.out.println("Enter the elements of first matrix ");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
a[i][j] = sc.nextInt();
}
}
// taking the elements of first matrix by user
System.out.println("Enter the elements of second matrix ");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
b[i][j] = sc.nextInt();
}
}
// matrix multiplication code
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
// making third matrix element to 0 for avoiding garbage values
c[i][j] = 0;
for (int k = 0; k < col1; k++)
{
// code to multiply two matrices
c[i][j] += a[i][k] * b[k][j];
}
}
}
// printing the multiplied matrix
System.out.println("\nMatrix after multiplication");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println("\n");
}
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Implementation in C++
// C++ program to find the matrix multiplication.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int row1, col1, row2, col2;
cout << "Enter the number of rows of first matrix ";
cin >> row1;
cout << "Enter the number of column of first matrix ";
cin >> col1;
cout << "Enter the number of rows of second matrix ";
cin >> row2;
cout << "Enter the number of column of second matrix ";
cin >> col2;
if (col1 != row2)
{
cout << " -----please enter valid inputs----";
}
else
{
int a[row1][col1], b[row2][col2], c[row1][col2];
cout << "Enter the elements of first matrix\n";
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
cin >> a[i][j];
}
}
cout << "Enter the elements of second matrix\n";
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
cin >> b[i][j];
}
}
// matrix multiplication code
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
// making third matrix element to 0 for avoiding garbage values
c[i][j] = 0;
for (int k = 0; k < col1; k++)
{
// code to multiply two matrices
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout << "\nMatrix after multiplication" << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}
}
You can also try this code with Online C++ Compiler
Run Code
Output
Complexity analysis
Time complexity
Since we are using three loops to multiply the elements. Therefore the time complexity would be O(n1*m1*m2), where n1 is the size of the row of the first matrix and m1 is the size of the column of the first matrix and m2 is the size of the column of the second matrix. You can consider it approximately equal to O(n^3).
Space complexity
Since we are using a new matrix of the size of n3 and m3 to store the elements therefore the space complexity would be O(n3*m3), n is the size of the row of the new matrix and m is the size of the column of the new matrix. You can consider it approximately equal to O(n*m).
Also see, Rabin Karp Algorithm
Frequently Asked Questions
What is a rectangular matrix?
The rectangular matrix is ​​a type of matrix. In this matrix, the elements are arranged in rows and columns. The placement of the elements in the matrix represents a rectangle. Therefore, it is called a rectangular matrix.
What is a horizontal matrix?
A matrix of degree m × n is called a horizontal matrix if n> m, where m is equal to the number of rows and n is equal to the number of columns.
Matrix example- { {1,2,3,4},
{2,5,1,1} }
In the example matrix shown below, the number of rows (n) = 2 and the number of columns (m) = 4.
What is the important condition to multiply the matrix?
Two matrices can only be multiplied when the number of columns of the first matrix and the number of rows of the second matrix are the same. For example, the 3*4 and 4*3 matrix is possible to multiply but the 2*3 and 2*4 matrix is not possible to multiply.
Conclusion
Matrix multiplication is a fundamental operation in linear algebra and various applications. By multiplying two matrices, we get a resultant matrix that combines their data. This process is essential for computer graphics, scientific computing, and data analysis, enabling complex transformations and computations in an efficient and systematic manner.
Refer to Code360 to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.