C++ Program to Demonstrate Matrix Addition

«
»

This is a C++ Program to Demonstrate Matrix Addition.

Problem Description

The program adds two matrices and prints the resultant output.

Problem Solution

1. The program checks the number of rows and columns of the two matrices.
2. If they are not equal, the matrices cannot be added and the program is exited.
3. Else the matrices are added using for loops.
4. The resultant matrix is then printed.
5. Exit.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Add Two Matrices and Print the Output. The program output is shown below.

  1. #include<iostream>
  2. using namespace std; 
  3. int main ()
  4. {
  5.     int m, n, p, q, i, j, A[5][5], B[5][5], C[5][5];
  6.     cout << "Enter rows and column of matrix A : ";
  7.     cin >> m >> n;
  8.     cout << "Enter rows and column of matrix B : ";
  9.     cin >> p >> q; 
  10.     if ((m != p) && (n != q))
  11.     {
  12.         cout << "Matrices cannot be added!";
  13.         exit(0);
  14.     }	
  15.     cout << "Enter elements of matrix A : ";
  16.     for (i = 0; i < m; i++)
  17.         for (j = 0; j < n; j++)
  18.             cin >> A[i][j];
  19. 	cout << "Enter elements of matrix B : ";
  20.     for (i = 0; i < p; i++)
  21.         for (j = 0; j < q; j++)
  22.             cin >> B[i][j];		
  23.     for (i = 0; i < m; i++)
  24.         for (j = 0; j < n; j++)
  25.             C[i][j] = A[i][j] + B[i][j];
  26.     cout << "Sum of matrices\n";
  27.     for (i = 0; i < m; i++)
  28.     {    for (j = 0; j < n; j++)
  29.             cout << C[i][j] << "  ";	
  30.         cout << "\n";
  31.     }
  32.     return 0;
  33. }
Program Explanation

1. The user is initially asked to enter the number of rows and columns of both the matrices.
2. If number of rows and columns of matrix A are not equal to number of rows and columns of matrix B respectively, then matrices cannot be added. The program is exited.
3. Else if they are equal, the elements of both the matrices are entered and added.
4. The resultant matrix C is then printed.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter rows and column of matrix A : 2 2                                                                                          
Enter rows and column of matrix B : 2 2                                                                                          
Enter elements of matrix A : 1 2 3 4                                                                                             
Enter elements of matrix B : 4 3 2 1                                                                                             
Sum of matrices                                                                                                                  
5  5                                                                                                                             
5  5
 
Case 2 :
Enter rows and column of matrix A : 2 3                                                                                        
Enter rows and column of matrix B : 3 2                                                                                        
Matrices cannot be added!
 
Case 3 :
Enter rows and column of matrix A : 1 3                                                                                        
Enter rows and column of matrix B : 1 3                                                                                        
Enter elements of matrix A : 0 2 7                                                                                             
Enter elements of matrix B : 0 3 3                                                                                             
Sum of matrices                                                                                                                
0  5  10

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

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

Take C++ Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement

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 & technical discussions at Telegram SanfoundryClasses.