C++ Program to Find Dot Product of Two Matrices

This is a C++ Program to Calculate Dot Product of Two Matrices.

Problem Description

The program takes two matrices and calculates the dot product.

Problem Solution

1. The program takes the size of the two matrices.
2. If they are not equal, dot product cannot be found and the program is exited.
3. Else the dot product of the matrices is calculated.
4. The result is printed.
5. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Calculate Dot Product of Two Matrices. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int i, j, m, n, p, q;
  6.     int A[10][10], B[10][10], C[10];
  7.     cout << "Enter number of rows and columns of matrix A : ";
  8.     cin >> m >> n;
  9.     cout << "Enter number of rows and columns of matrix B : ";
  10.     cin >> p >> q;
  11.     if ((m != p) && (n != q))
  12.     {
  13.         cout << "Dot product cannot be found as matrices are not of same size!";
  14.         exit(0);
  15.     }
  16.     cout << "Enter elements of matrix A : ";
  17.     for (i = 0; i < m; i++)
  18.         for (j = 0; j < n; j++)
  19.             cin >> A[i][j];
  20.     cout << "Enter elements of matrix B : ";
  21.     for (i = 0; i < m; i++)
  22.         for (j = 0; j < n; j++)
  23.             cin >> B[i][j];
  24.     for (i = 0; i < m; i++)
  25.     {
  26.         C[i] = 0;
  27.         for (j = 0; j < n; j++)
  28.             C[i] +=  A[i][j] * B[i][j];
  29.  
  30.     }
  31.     // Printing matrix A //
  32.     cout << "Matrix A : \n ";
  33.     for (i = 0; i < m; i++)
  34.     {
  35.         for (j = 0; j < n; j++)
  36.             cout << A[i][j] << " ";
  37.         cout << "\n ";
  38.     }
  39.  
  40.     // Printing matrix B //
  41.     cout << "Matrix B : \n ";
  42.     for (i = 0; i < m; i++)
  43.     {
  44.         for (j = 0; j < n; j++)
  45.             cout << B[i][j] << " ";
  46.         cout << "\n ";     
  47.     }
  48.     cout << "Dot product : \n ";
  49.     for (i = 0; i < m; i++)
  50.        cout << C[i] << " ";
  51.     return 0;
  52. }
Program Explanation

1. The user is asked to enter the number of rows and columns of the two matrices.
2. If the corresponding rows and columns are not equal, then dot product can’t be calculated. The program is exited.
3. Else, elements of both the matrices are asked to enter and stored int the arrays ‘A’ and ‘B’.
4. The corresponding columns of A and B are multiplied and added which gives the dot product of that column as each column is created as a vector.
5. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns of matrix A : 3 3
Enter number of rows and columns of matrix B : 3 3                                                                               
Enter elements of matrix A : 1 2 3 4 5 6 7 8 9
Enter elements of matrix B : 9 8 7 6 5 4 3 2 1
Matrix A :
 1 2 3
 4 5 6
 7 8 9                                                                                                                            
Matrix B :
 9 8 7  
 6 5 4 
 3 2 1
Dot product :  
 46 73 46
 
Case 2 :
Enter number of rows and columns of matrix A : 1 3
Enter number of rows and columns of matrix B : 3 1
Dot product cannot be found as matrices are not of same size!
 
Case 3 :
Enter number of rows and columns of matrix A : 2 2
Enter number of rows and columns of matrix B : 2 2
Enter elements of matrix A : 10 20 30 40
Enter elements of matrix B : 4 3 2 1
Matrix A : 
 10 20
 30 40
Matrix B :                                                                                                                       
 4 3  
 2 1
Dot product :                                                                                                                    
 100 100

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.