

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays
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*3 matrix has 3 rows and 3 columns as shown below −
8 6 3 7 1 9 5 1 9
A program that multiplies two matrices using multidimensional arrays is as follows.
Example
#include<iostream> using namespace std; int main() { int product[10][10], r1=2, c1=3, r2=3, c2=3, i, j, k; int a[2][3] = { {2, 4, 1} , {2, 3, 9} }; int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} }; 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; } } 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 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} };
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 Questions & Answers
- Java Program to Multiply to Matrix Using Multi-Dimensional Arrays
- C++ Program to Add Two Matrix Using Multi-dimensional Arrays
- Java Program to Add Two Matrix Using Multi-Dimensional Arrays
- Multi Dimensional Arrays in Javascript
- Dump Multi-Dimensional arrays in Java
- Does Java support multi-dimensional Arrays?
- Flattening multi-dimensional arrays in JavaScript
- Java Program to convert array to String for one dimensional and multi-dimensional arrays
- Get the Inner product of two multi-dimensional arrays in Python
- How to define multi-dimensional arrays in C#?
- How to initialize multi-dimensional arrays in C#?
- Sort in multi-dimensional arrays in JavaScript
- How to define multi-dimensional arrays in C/C++?
- Reduce a multi-dimensional array and multiply elements in Numpy
- How to multiply two Arrays in JavaScript?