
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Multiply two Matrices by Passing Matrix to Function
A matrix is a rectangular array of numbers that is arranged in the form of rows and columns.
An example of a matrix is as follows.
A 3*4 matrix has 3 rows and 4 columns as shown below.
8 6 3 5 7 1 9 2 5 1 9 8
A program that multiplies two matrices by passing the matrices to functions is as follows.
Example
#include<iostream> using namespace std; void MatrixMultiplication(int a[2][3],int b[3][3]) { int product[10][10], r1=2, c1=3, r2=3, c2=3, i, j, k; if (c1 != r2) { cout<<"Column of first matrix should be equal to row of second matrix"; } else { cout<<"The first matrix is:"<<endl; for(i=0; i<r1; ++i) { for(j=0; j<c1; ++j) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; cout<<"The second matrix is:"<<endl; for(i=0; i<r2; ++i) { for(j=0; j<c2; ++j) cout<<b[i][j]<<" "; cout<<endl; } cout<<endl; for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { product[i][j] = 0; } for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) for(k=0; k<c1; ++k) { product[i][j]+=a[i][k]*b[k][j]; } cout<<"Product of the two matrices is:"<<endl; for(i=0; i<r1; ++i) { for(j=0; j<c2; ++j) cout<<product[i][j]<<" "; cout<<endl; } } } int main() { int a[2][3] = { {2, 4, 1} , {2, 3, 9} }; int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} }; MatrixMultiplication(a,b); return 0; }
Output
The first matrix is: 2 4 1 2 3 9 The second matrix is: 1 2 3 3 6 1 2 9 7 Product of the two matrices is: 16 37 17 29 103 72
In the above program, the two matrices a and b are initialized in the main() function as follows.
int a[2][3] = { {2, 4, 1} , {2, 3, 9} }; int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} };
The function MatrixMultiplication() is called with the values of a and b. This is seen below.
MatrixMultiplication(a,b);
In the function MatrixMultiplication(), if the number of columns in the first matrix are not equal to the number of rows in the second matrix then multiplication cannot be performed. In this case an error message is printed. It is given as follows.
if (c1 != r2) { cout<<"Column of first matrix should be equal to row of second matrix"; }
Both the matrices a and b are displayed using a nested for loop. This is demonstrated by the following code snippet.
cout<<"The first matrix is:"<<endl; for(i=0; i<r1; ++i) { for(j=0; j<c1; ++j) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; cout<<"The second matrix is:"<<endl; for(i=0; i<r2; ++i) { for(j=0; j<c2; ++j) cout<<b[i][j]<<" "; cout<<endl; } cout<<endl;
After this, the product[][] matrix is initialized to 0. Then a nested for loop is used to find the product of the 2 matrices a and b. This is demonstrated in the below code snippet.
for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { product[i][j] = 0; } for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) for(k=0; k<c1; ++k) { product[i][j]+=a[i][k]*b[k][j]; }
After the product is obtained, it is printed. This is shown as follows.
cout<<"Product of the two matrices is:"<<endl; for(i=0; i<r1; ++i) { for(j=0; j<c2; ++j) cout<<product[i][j]<<" "; cout<<endl; }
- Related Articles
- Golang Program to Multiply two Matrices by Passing Matrix to a Function
- Swift Program to Multiply two Matrices by Passing Matrix to a Function
- C# program to multiply two matrices
- Java program to multiply two matrices.
- Python program to multiply two matrices
- Program to multiply two matrices in C++
- How to multiply two matrices by elements in R?
- Swift Program to Multiply two Matrices Using Multi-dimensional Arrays
- How to Multiply Two Matrices using Python?
- Golang Program to Add Two Complex Numbers by Passing Class to a Function
- How to multiply two matrices using pointers in C?
- C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays
- How to multiply corresponding values from two matrices in R?
- How can Tensorflow be used to multiply two matrices using Python?
- Java program to add two matrices.
