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


January 20, 2023, Learn eTutorial
1282

Here we discuss a C++ program to add two matrices to a two-dimensional array.

What is a matrix?

The matrix can be defined as an array of arrays or multidimensional arrays in a table format like rows and columns where the data is stored. For example, mat[r][c]

Where,

  • mat is the matrix name
  • r is the number of rows 
  • c is the number of columns

The size or the order of a matrix is defined as the number of rows and columns that the matrix has. A matrix with 'a' rows and 'b' columns is called an 'a × b' matrix.

How to add two matrices?

Two matrices can be added together only if the order of the two matrices are the same. For adding, just find sum of the corresponding entries, and place this sum in the position of the result matrix.

C++ program to add two matrices using multi-dimensional arrays.

Here the user is asked to enter the number of rows r and the number of columns c. The value of r and c should be less than 10 here for this C++ program.

Read the elements into the first array from the user using a nested for loop and add it to the first matrix. Similarly read elements for the second matrix, b [i] [j].

Access each corresponding element from both arrays and add the elements and store it in the corresponding position of another array called sum [i] [j] . for loop can be used for accessing the elements.

Sum[i][j] = a[i][j] + b[i][j].

Display the matrix sum.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables r , c, I, j; and array a[100][100], b[100][100], sum[100][100];

Step 5: Ask the user to enter the number of rows ( 1 to 100 ).

Step 6: read the number into the variable r;

Step 7: Ask the user to enter the number of columns (1 to 10);

Step 8: read the number into the variable c;

Step 9: Ask the user to enter the elements of the 1st matrix.

Step 10: Read the elements into the array a[100][100];

Step 11: Ask the user to enter the elements of the 2nd matrix;

Step 12: Read the elements into the array b[100][100];

Step 13: Get the first elements of both matrix a[100][00] and b[100][100] and add the elements and store them in the first position of the matrix sum[100][100].

Step 14: Continue step 13 until a[r][c] and b[r][c] and store the result in sum[r][c];

Step 15: Display sum[100][100]

Step 16: exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main()
{
    int r, c, a[100][100], b[100][100], sum[100][100], i, 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(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element a" << i + 1 << j + 1 << " : ";
           cin >> a[i][j];
       }

    // Storing elements of second matrix entered by user.
    cout << endl << "Enter elements of 2nd matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element b" << i + 1 << j + 1 << " : ";
           cin >> b[i][j];
       }

    // Adding Two matrices
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
            sum[i][j] = a[i][j] + b[i][j];

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

    return 0;
}
                                      

OUTPUT

Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2
Enter elements of 1st matrix: 
Enter element a11 : 4
Enter element a12 : 5
Enter element a21 : 7
Enter element a22 : 9
Enter elements of 2nd matrix: 
Enter element b11 : 42
Enter element b12 : 8
Enter element b21 : 23
Enter element b22 : 6
Sum of two matrix is: 
46  13  
30  15