In this post, you will learn ** how to Multiply Two Matrices Using Multi-dimensional Arrays** using C++ language.

This lesson will teach you how to Multiply Two Matrices Using Multi-dimensional Arrays, using mathematical function, and the for loop statement using the C++ Language. Letâ€™s look at the below source code.

## How to Multiply Two Matrices Using Multi-dimensional Arrays?

RUN CODE SNIPPET## Source Code

#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

The statementsÂ **#include<iostream>, using namespace std, int main**Â are the main factors that **support** the function of the source code.

Now we can look into the working and layout of the codeâ€™s function.

- Declare theÂ
**variablesÂ***product, r1, c1, r2, c2, i, j, k, a, bÂ*asÂ**integers.Â**The variablesÂ**r1, r2, c1, c2Â**hold the size of the rows and columns of the first and secondÂ**array.**And declare the variable**product**as an array by using the**[ ]**brackets to hold the size of the array. - Declare the variablesÂ
**a**andÂ**b**as**arrays**and assign the array values using the**{ }**braces.*Note: The multiplication of two arrays is possible only when the size of the column of the first matrix is equal to the size of the row of the second matrix.Â* - Declare aÂ
**decision making if statement**with the condition`(c1 != r2)`

to verify the above rule. In the else statement usingÂ**for loopÂ**statement with a condition to check the size of the row and column display the**first array.** - This is followed by a similarÂ
**for loopÂ**statement with a condition, to display theÂ**second array.**Next to execute the multiplication section insertÂ**for loop**statements as follows

for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) - The above for loop statements is used to check the size of the row and the column and assign an array to hold the values of the answer. Next declare threeÂ
**for loop statementsÂ**with the condition to verify the size of the row and column of the first two arrays and the new array created to hold the value of the answer. - In the body of the loop insert the following equation to find the
**product of the two arrays**product[i][j]+=a[i][k]*b[k][j]; - Finally declare anotherÂ
**for loopÂ**statement with the condition to check the size of the rows and column, to display the answer of the**product of the two arraysÂ**using the output functionÂ**cout.**