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

Matrices are a fundamental data structure in many fields, and adding two matrices is a fundamental operation that can be useful in various applications.

To start, let’s define what a matrix is. A matrix is a two-dimensional array of numbers.

Each number in the matrix is referred to as an element. The number of rows and columns in a matrix is called its dimensions.

To add two matrices, we need to make sure that they have the same dimensions.

If they don’t have the same dimensions, the addition operation cannot be performed.

The addition of two matrices is done by adding each element of the first matrix with the corresponding element of the second matrix. The resulting sum is then placed in the corresponding element of a third matrix.

To implement this in C++, we will use multi-dimensional arrays.

We will define three two-dimensional arrays of the same dimensions: the two matrices to be added, and the resulting sum matrix.

We will then loop through each element of the matrices and add the corresponding elements, storing the sum in the corresponding element of the sum matrix.

Let’s take a look at the code to see how this is done in practice:

#include <iostream>

using namespace std;

int main()
{
    int rows, columns;
    
    cout << "Enter the number of rows: ";
    cin >> rows;
    
    cout << "Enter the number of columns: ";
    cin >> columns;
    
    int matrix1[rows][columns];
    int matrix2[rows][columns];
    int sumMatrix[rows][columns];
    
    cout << "Enter the elements of matrix 1:\n";
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<columns; j++)
        {
            cin >> matrix1[i][j];
        }
    }
    
    cout << "Enter the elements of matrix 2:\n";
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<columns; j++)
        {
            cin >> matrix2[i][j];
        }
    }
    
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<columns; j++)
        {
            sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    
    cout << "The sum of the two matrices is:\n";
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<columns; j++)
        {
            cout << sumMatrix[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

In this program, we first ask the user to enter the number of rows and columns for the matrices.

We then define the three two-dimensional arrays: matrix1, matrix2, and sumMatrix.

We then ask the user to enter the elements of the first matrix and the second matrix.

We then loop through each element of the matrices and add the corresponding elements, storing the sum in the corresponding element of the sum matrix. Finally, we print out the resulting sum matrix.

In conclusion, adding two matrices using multi-dimensional arrays in C++ is a straightforward process.

By looping through each element of the matrices and adding the corresponding elements, we can create a new matrix that represents the sum of the two original matrices.

This process can be useful in many applications, such as image processing, linear algebra, and more.