BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Add two Matrices

Last Updated: September 4, 2017 by Chaitanya Singh | Filed Under: C++ Programs

In this tutorial, we will see three ways to add two Matrices in C++ – 1) Using a simple C++ program without using function 2) Adding two matrices using function 3) Using class and function.

A Simple C++ program to add two Matrices

Here we are asking user to input number of rows and columns of matrices and then we ask user to enter the elements of both the matrices, we are storing the input into a multidimensional array for each matrix and after that we are adding corresponding elements of both the matrices and displaying them on screen.

#include<iostream>
using namespace std;

int main()
{
   int row, col, m1[10][10], m2[10][10], sum[10][10];

   cout<<"Enter the number of rows(should be >1 and <10): ";
   cin>>row;
   cout<<"Enter the number of column(should be >1 and <10): ";
   cin>>col;
   cout << "Enter the elements of first 1st matrix: ";
   for (int i = 0;i<row;i++ ) {
     for (int j = 0;j < col;j++ ) {
       cin>>m1[i][j];
     }
   }
   cout << "Enter the elements of first 1st matrix: ";
   for (int i = 0;i<row;i++ ) {
     for (int j = 0;j<col;j++ ) {
       cin>>m2[i][j];
     }
   }

   cout<<"Output: ";
   for (int i = 0;i<row;i++ ) {
      for (int j = 0;j<col;j++ ) {
        sum[i][j]=m1[i][j]+m2[i][j];
        cout<<sum[i][j]<<" ";
      }
   }

   return 0;
}

Output:

Enter the number of rows(should be >1 and <10): 2
Enter the number of column(should be >1 and <10): 3
Enter the elements of  first 1st matrix: 1 2 3 4 5 6
Enter the elements of  first 1st matrix: 5 5 5 5 5 5
Output: 6 7 8 9 10 11

2) Program to Add two Matrices using function

In this program, instead of writing everything in main() function, we have created a user defined function sum(int, int) in which we pass number of rows and number of columns entered by user. This function creates two matrices based on the passed rows and columns, add the corresponding elements of matrices and then displays the result.

#include<iostream>
using namespace std;
void sum(int, int);

int main(){
   int row, col;
   cout<<"Enter the number of rows(should be >1 and <10): ";
   cin>>row;
   cout<<"Enter the number of column(should be >1 and <10): ";
   cin>>col;

   sum(row, col);
   return 0;
}
void sum(int r, int c){
   int m1[r][c], m2[r][c], s[r][c];
   cout << "Enter the elements of  first 1st matrix: ";
   for (int i = 0;i<r;i++ ) {
     for (int j = 0;j < c;j++ ) {
       cin>>m1[i][j];
     }
   }

   cout << "Enter the elements of  first 1st matrix: ";
   for (int i = 0;i<r;i++ ) {
     for (int j = 0;j<c;j++ ) {
       cin>>m2[i][j];
     }
   }
   cout<<"Output: ";
   for (int i = 0;i<r;i++ ) { 
     for (int j = 0;j<c;j++ ) {
       s[i][j]=m1[i][j]+m2[i][j];
       cout<<s[i][j]<<" ";
     }
   }
}

Output:

Enter the number of rows(should be >1 and <10): 2
Enter the number of column(should be >1 and <10): 3
Enter the elements of  first 1st matrix: 1 1 1 1 1 1
Enter the elements of  first 1st matrix: 10 20 30 40 50 60
Output: 11 21 31 41 51 61

Addition using Class and function

The same logic that we have seen above can be applied while writing this program using class and function.

#include<iostream>
using namespace std;

class Add{
  public:
    void sum(int r, int c){
      int m1[r][c], m2[r][c], s[r][c];
      cout << "Enter the elements of  first 1st matrix: ";
      for (int i = 0;i<r;i++ ) {
        for (int j = 0;j < c;j++ ) {
          cin>>m1[i][j];
        }
      }
      cout << "Enter the elements of  first 1st matrix: ";
      for (int i = 0;i<r;i++ ) {
        for (int j = 0;j<c;j++ ) {
          cin>>m2[i][j];
        }
      }
      cout<<"Output: ";
      for (int i = 0;i<r;i++ ) {
        for (int j = 0;j<c;j++ ) {
          s[i][j]=m1[i][j]+m2[i][j];
          cout<<s[i][j]<<" ";
        }
      }
    }
};
int main(){
   int row, col;
   cout<<"Enter the number of rows(should be >1 and <10): ";
   cin>>row;
   cout<<"Enter the number of column(should be >1 and <10): ";
   cin>>col;
   Add obj;
   obj.sum(row, col);
   return 0;
}

Output:

Enter the number of rows(should be >1 and <10): 2
Enter the number of column(should be >1 and <10): 3
Enter the elements of  first 1st matrix: 1 1 1 1 1 1
Enter the elements of  first 1st matrix: 5 5 5 5 5 5
Output: 6 6 6 6 6 6

Top Related Articles:

  1. C++ Program to add two numbers
  2. C++ Program to display Armstrong Numbers between 1 and 1000
  3. C++ Program to find the sum of n natural numbers
  4. C++ Program to Display the Number Entered by User
  5. C++ Example Programs With Output

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap