C++

Two Matrix Addition in C++ – Detailed Instructions

In this article, we’ll look at three different C++ methods for adding two matrices: 1) Employing a straightforward C++ application without using functions 2) Using a function, adding two matrices 3) Making use of class and function

A straightforward C++ program that adds two matrices

Here, we ask the user to enter the number of rows and columns of matrices before asking them to enter the elements of both matrices. We then store the user’s input into a multidimensional array for each matrix before adding the corresponding elements of both matrices and displaying them on the 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) A program that uses a function to add two matrices

In this application, we’ve established a user-defined function called sum(int, int) where we send the number of rows and columns the user enters, as opposed to writing everything in the main() function. Based on the inputted rows and columns, this function constructs two matrices, adds the respective elements, and then displays the outcome.

#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

Utilizing class and function to add

When writing this program, class and function can be used to implement the same logic that we have seen earlier.

#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

Related Articles

Leave a Reply

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

Back to top button