C++ Program of Multiplication of Matrix Using 2D Array

ad+1


In this program we are going to multiply two matrices. For this purpose we will declare two arrays, we will take input in these arrays and then we will multiply them and store their result in a new array.

 

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], multi[10][10];
    int row1=0, column1=0, row2=0, column2=0;
  
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;

/* If columns of first matrix in not equal to rows of second matrix then error will occur, asking user to re-enter size of matrix. */
 
    while (column1!=row2)
    {
  
    cout << "Error Occured! column of first matrix not equal to row of second.";
      
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;
  
    }

/* Filling of first matrix. */

    cout<< "Enter elements of 1st matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column1; j++)
    {
        cout << "Enter element a" << i+1 << j+1 << " : ";
        cin >> a[i][j];
    }

/* Filling of second matrix. */
    cout <<"Enter elements of 2nd matrix: " << endl;
    for(int i=0; i<row2; ++i)
    for(int j=0; j<column2; ++j)
    {
        cout << "Enter element b" << i+1 << j+1 << " : ";
        cin >> b[i][j];
    }

/* Initializing array of multi.*/
    for(int i=0; i<row1; ++i)
    for(int j=0; j<column2; ++j)
    {
       multi[i][j]=0;
    }

/* Multiplying matrix a and b and storing in array of multi. */
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    for(int k=0; k<column1; k++)
    {
        multi[i][j]=multi[i][j] + a[i][k] * b[k][j];
    }

/* Displaying the result. */
    cout <<"Output Matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    {
        cout << " " << multi[i][j];
        if(j==column2-1)
            cout << endl;
    }
    return 0;
}

Output:


3 comments: Leave Your Comments

  1. Do You want to Sell your website or blog ? I am Hitesh Kumar Owner of https://www.sitesbay.com/ You can drop your response on www.facebook.com/sitesbay

    ReplyDelete
  2. I found nice article in your blog.thank you for useful info
    .

    ReplyDelete

  3. Very Informative and creative contents. This concept is a good way to enhance the knowledge. thanks for sharing.
    Continue to share your knowledge through articles like these, and keep posting more blogs.
    And more Information Data Labeling Service for Machine Learning

    ReplyDelete