04 January, 2013

C++ Program For Subtraction Of Two Matrices.

This Post Contains A C++ Program For Subtraction Of Two Matrices With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-loop' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program For Subtraction Of Two Matrices.

/* Declaring The Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{
    /* Declaration Of Variables */
    int a[10][10],b[10][10],d[10][10];
    int i, j, r, c, p, q;
   
    clrscr();
   
    /* Accepting Dimensions Of Matrix 'A' From User */
    cout << " Enter Order Of Matrix A : ";
    cin >> r >> c;
   
    /* Accepting Values For Matrix 'A' From User */
    cout << " Enter " << r*c << " Values : ";
    for(i=0; i<r; i++)
    {   
           for(j=0; j<c; j++)
       {
              cin >> a[i][j];
       }  
    }

    /* Accepting Dimensions Of Matrix 'B' From User */
    cout << " Enter Order Of Matrix B : ";
    cin>>p>>q;
   
    /* Accepting Values For Matrix 'B' From User */
    cout << " Enter " << p*q << " Values : ";
    for(i=0; i<p; i++)
    {   
           for(j=0; j<q; j++)
       {
              cin >> b[i][j];
       }  
    }

    /* Printing Values Of Matrix 'A' */
    cout << " \n Elements Of Array A : ";
    for(i=0; i<r; i++)
    {
       cout << " \n";
       for(j=0' j<c; j++)
       {
          cout << a[i][j] << "\t";
       }
    }

    /* Printing Values Of Matrix 'B' */
    cout << "\n Elements Of Array B : ";
    for(i=0; i<p; i++)
    {
       cout << "\n";
       for(j=0; j<q; j++)
       {
          cout<<b[p][q]<<"\t";
       }
    }

    /* Subtraction Of Two Matrices Will Take Place If & Only If 'rows' & 'columns' Of Two Matrices Are Equal */
    /* Checking If 'rows' & 'columns' Of Two Matrices Are 'Equal' */
    if( r==p && c==q )
    {
        for(i=0; i<r; i++)
        {
           for(j=0; j<q; j++)
           {
                  d[i][j] = a[i][j] - b[i][j];
               }
            }

        /* Printing Values Of Resultant Matrix i.e 'D' */
        cout << " \n Elements Of Resultant Array 'D' : ";
        for(i=0; i<p; i++)
        {
           cout<<"\n";
           for(j=0' j<q; j++)
           {
              cout << d[p][q] << "\t";
           }
        }
    }
    else
    {
        cout << " \n Subtraction Of Two Matrices Is Not Possible ";
    }
    getch();
}
/* End Of Main Program */

 # Note : Please Click Here To View Other Programs Related To Matrices.

Output :

Enter Order Of Matrix 'A' : 3  3
Enter 9 Values :  2  4  6  8  10  12  14  16  18

Elements Of Array 'A' :
2   4   6
8   10  12
14  16  18

Enter Order Of Matrix 'B' : 3  3
Enter 9 Values :  1  2  3  4  5  6  7  8  9

Elements Of Array 'B' :
1  2  3
4  5  6
7  8  9

Elements Of Resultant Array 'D' :
1  2  3
4  5  6
7  8  9




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.