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

In this program, we will learn how to add two matrix using multi-dimensional array in C++ programming language.

A matrix is a rectangular array of numbers that are arranged in the form of rows and columns.
Example:-
A 3*3 matrix has 3 rows and 3 columns as shown below −
5 6 7
3 4 9
7 6 1

## C++ Programming Code to Add Two Matrix Using Multi-dimensional Array

We will use the multi-dimensional array to store the matrices (a and b).

Firstly, User will be asked to enter the number of rows it required in the matrices and the same for the columns.

And then we will use the for loop to traverse through the multi-dimensional array to store the values.

Also nested for loop will be used to add the individual elements of the multi-dimensional array.

And then save the sum of the matrices in sum multi-dimensional array.

### Code:-

```#include <iostream>
using namespace std;

int main()
{
int row, col;
int a, b, sum;

cout << "Enter number of rows (between 1 and 100): ";
cin >> row;

cout << "Enter number of columns (between 1 and 100): ";
cin >> col;

cout << "Enter the elements of matrix a: " << endl;
for (int i = 0;i<row;i++ ) {
for (int j = 0;j < col;j++ ) {
cin >> a[i][j];
}
}
cout << "Enter the elements of matrix b: " << endl;
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
cin >> b[i][j];
}
}

for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the sum matrix.
cout << endl << "Sum of two matrices is: " << endl;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
cout << sum[i][j] << "  ";
if(j == col - 1)
cout << endl;
}

return 0;
}```

### Output:-

Enter number of rows (between 1 and 100): 3
Enter number of columns (between 1 and 100): 3
Enter the elements of matrix a:
5 6 7
3 4 9
7 6 1
Enter the elements of matrix b:
7 8 4
9 6 2
5 8 9
Sum of two matrices is:
12 14 11
12 10 11
12 14 10

### You can learn about many other C++ Programs Here.

#### Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

#### Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.