codescracker
c++

C++ Program to Subtract Two Matrices



« Previous Program Next Program »

In this article, you will learn and get code to subtract two matrices using a C++ program. Here are the list of programs on matrix subtraction:

But before going through these programs, if you're not aware about the topic, then refer to matrix subtraction article to get every required things.

Note - In matrix subtraction, same row and column element of second matrix gets subtracted from same row and column element of first matrix. Let me shows you the example:

1  2  3     0  1  3     1  1  0
4  5  6  -  3  1  2  =  1  4  4
7  8  9     1  2  3     6  6  6

As you can see, the element at first row first column of second matrix (0) gets subtracted from element at first row first column of first matrix (1). Similarly element at first row second column of second matrix (1) gets subtracted from element at first row second column of first matrix (2), and so on.

Subtract Two 3*3 Matrices

To subtract two matrices in C++ programming, you have to ask from user to enter the two 3*3 matrices. That is 9 elements (numbers) for first and 9 elements for second matrix. Then subtract the second matrix from first as shown in the program given below:

#include<iostream>
using namespace std;
int main()
{
    int matOne[3][3], matTwo[3][3], matSub[3][3], i, j;
    cout<<"Enter 9 Elements for First Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>matOne[i][j];
    }
    cout<<"Enter 9 Elements for Second Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>matTwo[i][j];
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            matSub[i][j] = matOne[i][j] - matTwo[i][j];
    }
    cout<<"\nThe New Matrix (Subtraction Result) is:\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cout<<matSub[i][j]<<"  ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program subtract two matrices

Now supply inputs say 9, 8, 7, 6, 5, 4, 3, 2, 1 as 9 elements for first matrix, and 8, 7, 6, 5, 4, 3, 2, 1, 0 as 9 elements for second matrix. Here is the sample output after subtracting two given matrices:

subtract two matrices c++

When user enters 9, 8, 7, 6, 5, 4, 3, 2, 1 as 9 elements for first matrix, then it gets stored in matOne[][] in this way:

Whereas the 9 elements say 8, 7, 6, 5, 4, 3, 2, 1, 0 gets stored in second matrix say matTwo[][] in similar way, that is:

Now the matrix subtraction with following block of code gets performed:

for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        matSub[i][j] = matOne[i][j] - matTwo[i][j];
}

When we evaluate this block of code, then its dry run goes like:

Now on putting the number or values for each:

As you can see that the subtraction result is 1 in all cases, therefore in every indexes of matSub[][], 1 gets stored.

Subtract Two Matrices and Print A-B=C

This program is similar to previous program. But there is an extra feature included in this program. That extra feature is, this program shows the generalized way of matrix subtraction with A-B=C. Here A is first matrix, B is second matrix, and C is the subtraction result of both matrices.

#include<iostream>
using namespace std;
int main()
{
    int matOne[3][3], matTwo[3][3], matSub[3][3], i, j;
    cout<<"Enter 9 Elements for First Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>matOne[i][j];
    }
    cout<<"Enter 9 Elements for Second Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>matTwo[i][j];
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            matSub[i][j] = matOne[i][j] - matTwo[i][j];
    }
    cout<<"\nSubtraction of Two Matrix is:\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cout<<matOne[i][j]<<"  ";
        if(i==1)
            cout<<"  -\t";
        else
            cout<<"\t";
        for(j=0; j<3; j++)
            cout<<matTwo[i][j]<<"  ";
        if(i==1)
            cout<<"  =\t";
        else
            cout<<"\t";
        for(j=0; j<3; j++)
            cout<<matSub[i][j]<<"  ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input 1, 2, 3, 4, 5, 6, 7, 8, 9 as first matrix elements, and 0, 1, 2, 1, 2, 3, 1, 5, 3 as second matrix elements:

c++ matrix subtraction

Subtract Two Matrices of Given Size

This program allows user to define the size for matrix before entering the elements. Rest of the things are similar to very first program of this article:

#include<iostream>
using namespace std;
int main()
{
    int rowOne, colOne, rowTwo, colTwo, i, j;
    int matOne[10][10], matTwo[10][10], matSub[10][10];
    cout<<"Enter Row and Column Size for First Matrix: ";
    cin>>rowOne>>colOne;
    cout<<"Enter Row and Column Size for Second Matrix: ";
    cin>>rowTwo>>colTwo;
    if(rowOne==rowTwo && colOne==colTwo)
    {
        cout<<"\nEnter "<<rowOne*colOne<<" Elements for First Matrix: ";
        for(i=0; i<rowOne; i++)
        {
            for(j=0; j<colOne; j++)
                cin>>matOne[i][j];
        }
        cout<<"\nEnter "<<rowTwo*colTwo<<" Elements for Second Matrix: ";
        for(i=0; i<rowTwo; i++)
        {
            for(j=0; j<colTwo; j++)
                cin>>matTwo[i][j];
        }
        cout<<"\nThe New Matrix (Subtraction Result) is:\n";
        for(i=0; i<rowOne; i++)
        {
            for(j=0; j<colOne; j++)
            {
                matSub[i][j] = matOne[i][j] - matTwo[i][j];
                cout<<matSub[i][j]<<"  ";
            }
            cout<<endl;
        }
    }
    else
        cout<<"\nRow and Column Size of Both Matrix Mismatched!";
    cout<<endl;
    return 0;
}

Now supply the following inputs:

After supplying these inputs, here is the sample output:

c++ subtract two matrices of given size

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker