C++ Program to Subtract Two Matrices

This C++ Program which demonstrates subtraction of two matrices of same order. The program initializes the matrices, subtracts each corresponding element of the two matrices and puts the value in the third matrix.

Here is source code of the C++ program which demonstrates subtraction of two matrices of same order. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ program to demonstrate matrix subtraction
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int mat1[3][3], mat2[3][3], mat3[3][3];
  10.  
  11.     /* Initializing Mat1 and Mat2 */
  12.     for (int i = 0; i < 3; i++)
  13.     {
  14.         for (int j = 0; j < 3; j++)
  15.         {
  16.             mat1[i][j] = i + 1;
  17.             mat2[i][j] = j + 1;		
  18.         }
  19.     }
  20.  
  21.     /* Adding matrices Mat1 and Mat2 */
  22.     for (int i = 0; i < 3; i++)
  23.     {
  24.         for (int j = 0; j < 3; j++)
  25.         {
  26.             mat3[i][j] = mat2[i][j] - mat1[i][j];	
  27.         }
  28.     }
  29.  
  30.     cout << "First Matrix : " << endl;
  31.     for (int i = 0; i < 3; i++)
  32.     {
  33.         for (int j = 0; j < 3; j++)
  34.         {
  35.             cout << mat1[i][j] << "\t";	
  36.         }
  37.         cout << endl;
  38.     }
  39.  
  40.     cout << "Second Matrix : " << endl;
  41.     for (int i = 0; i < 3; i++)
  42.     {
  43.         for (int j = 0; j < 3; j++)
  44.         {
  45.             cout << mat2[i][j] << "\t";	
  46.         }
  47.         cout << endl;
  48.     }
  49.  
  50.     cout << "Third Matrix = Second Matrix - First Matrix : " << endl;
  51.     for (int i = 0; i < 3; i++)
  52.     {
  53.         for (int j = 0; j < 3; j++)
  54.         {
  55.             cout << mat3[i][j] << "\t";	
  56.         }
  57.         cout << endl;
  58.     }
  59. }

$ g++ main.cpp
$ ./a.out
First Matrix : 
1	1	1	
2	2	2	
3	3	3	
Second Matrix : 
1	2	3	
1	2	3	
1	2	3	
Third Matrix = Second Matrix - First Matrix : 
0	1	2	
-1	0	1	
-2	-1	0

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

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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.