banner



How To Add Two 2d Arrays In C++

C++ Program to Add 2 Matrices (2d Arrays)

Hello Everyone!

In this tutorial, we will larn how to notice the Addition of 2 Matrices (2D Arrays), in the C++ programming linguistic communication.

Matrix Addition:

Matrix Addition is a binary functioning that produces a single matrix as a effect by addition of the respective elements of the 2 matrices.

Constraint: For Matrix Improver, there is one necessary condition - Both the matrices should take the same dimensions i.e. aforementioned number of rows and columns.

The result matrix has the same dimension as that of the 2 matrices that have got added.

In general, an element a[i][j] of the production matrix is formed past adding the m1[]i[j] and m2[i][j], i.e. the 1st element of the matrix product is found by adding the first element of the both the matrices (corresponding elements), second element is formed by calculation 2nd elements of both the matrices and so on.

For meliorate understanding, refer to the well commented code given below.

Code:

            #include <iostream>  using namespace std;  int main() {     cout << "\n\nWelcome to Studytonight :-)\northward\north\n";     cout << " =====  Program to demonstrate Addition of ii Matrices ===== \n\n";      //loop variable i to iterate rows and j to iterate columns.     int row, col, i, j;      //Declaring the 3 matrices (2D arrays) m1-first matrix, m2- second matrix and sum- stores the addition of the two matrices     int m1[x][10], m2[10][10], sum[10][10];      cout << "\n\nEnter the number of Rows and Columns of matrix : ";     cin >> row >> col;      cout << "\nEnter the " << row * col << " elements of first matrix : \north";      for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             cin >> m1[i][j];         }     }      cout << "\nEnter the " << row * col << " elements of second matrix : \north";      for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             cin >> m2[i][j];         }     }      //calculating the sum matrix     for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             sum[i][j] = m1[i][j] + m2[i][j];         }     }      cout << "\due north\nThe first matrix is : \n";      for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             cout << m1[i][j] << "  ";         }         cout << endl;     }      cout << "\n\nThe second matrix is : \n";      for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             cout << m2[i][j] << "  ";         }         cout << endl;     }      cout << "\due north\nThe Sum matrix is : \n";      for (i = 0; i < row; i++) {         for (j = 0; j < col; j++) {             cout << sum[i][j] << "  ";         }          cout << endl;     }      cout << "\n\n";      return 0; }          

Output:

C++ Addition of two matrices

Nosotros promise that this post helped you lot develop amend understanding of the Matrix Addition concept and its implementation in C++. For any query, feel complimentary to reach out to us via the comments department down below.

Keep Learning : )



How To Add Two 2d Arrays In C++,

Source: https://www.studytonight.com/cpp-programs/cpp-program-to-add-two-matrices-2d-arrays

Posted by: palmerrecance.blogspot.com

0 Response to "How To Add Two 2d Arrays In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel