Algorithm
1. Start
2. Define a function prototype for matrix multiplication:
- `void multiplyMatrices(int mat1[][MAX_COLS], int mat2[][MAX_COLS], int result[][MAX_COLS], int rows1, int cols1, int rows2, int cols2);`
3. Inside the main function:
- Declare and initialize matrices `mat1`, `mat2`, and `result`.
- Input the dimensions (rows and columns) of matrices `mat1` and `mat2`.
4. Check if the multiplication is possible:
- If `cols1` is not equal to `rows2`, display an error message and exit.
5. Call the matrix multiplication function:
- `multiplyMatrices(mat1, mat2, result, rows1, cols1, rows2, cols2);`
6. Display the original matrices and the result matrix:
- Display the content of matrices `mat1`, `mat2`, and `result`.
7. End
8. Define the matrix multiplication function:
- `void multiplyMatrices(int mat1[][MAX_COLS], int mat2[][MAX_COLS], int result[][MAX_COLS], int rows1, int cols1, int rows2, int cols2) {`
- Check if the multiplication is possible.
- Use nested loops to iterate over rows and columns to perform matrix multiplication.
- Store the result in the `result` matrix.
- `}`
9. End
Code Examples
#1 Code Example- Multiply Matrix by Passing it to a Function
Code -
C++ Programming
#include <iostream>
using namespace std;
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);
int main()
{
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
while (columnFirst != rowSecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
}
// Function to take matrices data
enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to multiply two matrices.
multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to display resultant matrix after multiplication.
display(mult, rowFirst, columnSecond);
return 0;
}
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j;
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
cin >> firstMatrix[i][j];
}
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> secondMatrix[i][j];
}
}
}
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;
// Initializing elements of matrix mult to 0.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void display(int mult[][10], int rowFirst, int columnSecond)
{
int i, j;
cout << "Output Matrix:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << mult[i][j] << " ";
if(j == columnSecond - 1)
cout << endl << endl;
}
}
}
Copy The Code &
Try With Live Editor
Output
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.
Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2
Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4
Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4
Output Matrix:
24 29
6 25
Demonstration
C++ Programing Example to Multiply two Matrices by Passing Matrix to Function-DevsEnv