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.

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.

  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:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Check Armstrong Number using C++ programming language. This lesson will teach you...
  • C++
  • December 2, 2024
In this post, you will learn how to Calculate the Sum of Natural Numbers using C++. This lesson will teach...
  • C++
  • December 2, 2024
In this post, you’ll learn how to write a Program to Add Two Integers in C++. This lesson will help...
  • C++
  • November 30, 2024