Learn Programming and technology with ease @ developerpublish.com

HomeC++C++ Program to Multiply Two Matrices Using Multi-dimensional Arrays

# C++ Program to Multiply Two Matrices Using Multi-dimensional Arrays

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.

## 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.

1. 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.
2. 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.Â
3. 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.
4. 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)
5. 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.
6. 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];
7. 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.

Share:

### You May Also Like

#### C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022