C++ Program to Perform Matrix Multiplication using Multidimensional Arrays

by - June 06, 2019

C++ Program to Perform Matrix Multiplication using Multidimensional Arrays


Matrix Multiplication

In mathematics, matrix multiplication or matrix product is a binary operation that produces a matrix from two matrices. Two matrices are multipliable if number of columns of 1st matrix is equal to the number of rows of 2nd matrix. Learn How to Multiply two matrices

C++ Program to Perform Matrix Multiplication using Multidimensional Arrays. The program multiplies two matrices and prints the result matrix. The program takes two matrices and multiplies them. If number of columns of matrix A is equal to number of rows of matrix B, then these matrices can be Multiply.

Logic to Multiply two matrices

  1. Ask user to enter rows and columns for both matrices and make two multidimensional arrays
  2. Ask user to input their fields and store them in multidimensional arrays
  3. Display both matrices
  4. Multiply them and display the resultant matrix 

Write a C++ Program to Perform Matrix Multiplication using Multidimensional Arrays.


// C++ Program to Perform Matrix Multiplication using Multidimensional Arrays
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
                int row1, col1, row2, col2;
                cout << "Enter Number of Rows and Colums of 1st Matrix: ";
                cin >> row1 >> col1;
                int arr1[row1][col1]= {0};
                cout << "Enter Number of Rows and Colums of 2nd Matrix: ";
                cin >> row2 >> col2;
                cout << endl;
                int arr2[row2][col2]= {0};
                if(col1==row2) {
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col1; j++) {
                                                                cout<<"Enter A"<<i+1<<j+1<<": ";
                                                                cin>>arr1[i][j];
                                                }
                                }
                                cout<<"\n1st Matrix: "<<endl;
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col1; j++) {
                                                                cout<<arr1[i][j]<<"\t";
                                                }
                                                cout<<endl;
                                }
                                cout << endl;
                                for(int i=0; i<row2; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<"Enter B"<<i+1<<j+1<<": ";
                                                                cin>>arr2[i][j];
                                                }
                                }
                                cout<<"\n2nd Matrix: "<<endl;
                                for(int i=0; i<row2; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<arr2[i][j]<<"\t";
                                                }
                                                cout<<endl;
                                }
                                int arr3[row1][10]= {0};

                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                for(int k=0; k<col1; k++) {
                                                                                arr3[i][j]+=arr1[i][k]*arr2[k][j];
                                                                }
                                                }
                                }
                                cout<<"\nAfter Multiplication: "<<endl;
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<arr3[i][j]<<"\t";
                                                }
                                                cout<<endl;

                                }
                } else {
                                cout<<"\n\nThese Matrices Cannot Be Multiplied......"<<endl;
                                cout<<"\t\tEnter Again\n\n"<<endl;
                                main();
                }
}

Output

Enter Number of Rows and Colums of 1st Matrix: 2 3
Enter Number of Rows and Colums of 2nd Matrix: 3 2

Enter A11: 1
Enter A12: 2
Enter A13: 3
Enter A21: 4
Enter A22: 5
Enter A23: 6

1st Matrix:
1       2       3
4       5       6

Enter B11: 6
Enter B12: 5
Enter B21: 4
Enter B22: 3
Enter B31: 2
Enter B32: 1

2nd Matrix:
6       5
4       3
2       1

After Multiplication:
20      14
56      41


You May Also Like

0 comments