 # C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### C++ Program to Add Two Matrix Using Multi-dimensional Arrays

``` C++ Program to Add Two Matrix Using Multi-dimensional Arrays This program takes two matrices of order r*c and stores it in two-dimensional array. Then, the program adds these two matrices and displays it on the screen. In this program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 100 in this program. The user is asked to enter elements of two matrices (of order r*c). Then, the program adds these two matrices, saves it in another matrix (two-dimensional array) and displays it on the screen. #include <iostream> using namespace std; int main() { int r, c, a, b, sum, x, j; cout << "Enter number of rows (between 1 and 100): "; cin >> r; cout << "Enter number of columns (between 1 and 100): "; cin >> c; cout << endl << "Enter elements of 1st matrix: " << endl; // Storing elements of first matrix entered by user. for(x = 0; x < r; ++x) for(j = 0; j < c; ++j) { cout << "Enter element a" << x + 1 << j + 1 << " : "; cin >> a[x][j]; } // Storing elements of second matrix entered by user. cout << endl << "Enter elements of 2nd matrix: " << endl; for(x = 0; x < r; ++x) for(j = 0; j < c; ++j) { cout << "Enter element b" << x + 1 << j + 1 << " : "; cin >> b[x][j]; } // Adding Two matrices for(x = 0; x < r; ++x) for(j = 0; j < c; ++j) sum[x][j] = a[x][j] + b[x][j]; // Displaying the resultant sum matrix. cout << endl << "Sum of two matrix is: " << endl; for(x = 0; x < r; ++x) for(j = 0; j < c; ++j) { cout << sum[x][j] << " "; if(j == c - 1) cout << endl; } return 0; } ``` 