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

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

What is a matrix?

A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. Matrices are commonly written in box brackets. The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively. The size of a matrix is defined by the number of rows and columns that it contains. A matrix with m rows and n columns is called an m × n matrix.

How do subtract two matrices?

A matrix can only be subtracted from another matrix if the two matrices have the same dimensions. To subtract two matrices, just subtract the corresponding entries, and place this result in the corresponding position in the matrix results.

Multidimensional arrays in C++

In C++, we can create an array of an array, known as a multidimensional array. For example: int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
We can think of this array as a table with 3 rows and each row has 4 columns 
Three-dimensional arrays also work similarly. For example:
float x[2][4][3];
This array x can hold a maximum of 24 elements.
We can find out the total number of elements in the array simply by multiplying its dimensions:
2 x 4 x 3 = 24

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

Here the user is asked to enter the number of rows r and number of columns c. The value of r and c should be less than 100 here for this program. Read the elements into the first array from the user. For this use for loop. Initialize variable i to point the raw and j for point the column. The condition will be like i < r; and j < c. increment the value of and j by 1 on each iteration. a [i[ [j]
Similarly read elemnts for the second matrix, b [i] [j].
Access each corresponding element from both arrays and subtract the elements and store it in the corresponding position of another array called sub [i] [j] . for loop can be used for accessing the elements.
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