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


January 20, 2023, Learn eTutorial
1539

Here we discuss a C++ program to subtract two matrices in 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 of 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 do subtract two matrices?

We can do the matrix subtraction only when the order of both matrices matches. To subtract two matrices, we have to subtract the elements of the correct position of matrix A from the element of matrix B.

How to subtract two matrices using a C++ program?

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 100 here for this C++ program.

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

Access each corresponding element from both arrays of 'a' and 'b' and subtract the elements and store it in the corresponding position of another array called sub [i] [j] . nested for loop is used for the subtraction of each element in the table formated matrix.

Sub[i][j] = a[i] [j] - B[i][j]. Display the matrix sub.

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], sub[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 subtract the elements and store them in the first position of the matrix sub[100][100].

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

Step 15: Display sub[100][100]

Step 16: exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main()
{
    int r, c, a[100][100], b[100][100], sub[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)
            sub[i][j] = a[i][j] - b[i][j];

    // Displaying the resultant sum matrix.
    cout << endl << "Subtracted result of two matrix is: " << endl;
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
        {
            cout << sub[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 : 6
Enter element a12 : 9
Enter element a21 : 8
Enter element a22 : 4
Enter elements of 2nd matrix: 
Enter element b11 : 8
Enter element b12 : 6
Enter element b21 : 2
Enter element b22 : 4
Subtracted result of two matrix is: 
-2  3  
6  0