Thursday, 26 June 2014

C++ program to subtract one matrix from another matrix

C++ program to subtract one matrix from another matrix. Here I have given three programs to subtract one matrix from another matrix like using function, using pointers, using operator overloading etc.


C++ program to subtract one matrix from another matrix

#include<iostream>
using namespace std;
int main()
{
    int c[10][10],a[10][10],b[10][10],row,col;
    cout<<"Enter row and column of matrix (max 10*10): ";
    cin>>row>>col;
    cout<<"Enter matrix a1 : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
           cin>>a[i][j];
    cout<<"Enter matrix a2 : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
           cin>>b[i][j];
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
           c[i][j]=a[i][j]-b[i][j];
    cout<<"Difference of two matrix : "<<endl;
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
            cout<<c[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}



C++ program to subtract one matrix from another matrix using pointers

#include<iostream>
using namespace std;
int main()
{
    int *p,*q,row,col;
    cout<<"Enter row and column of matrix : ";
    cin>>row>>col;
    p=new int[row*col];
    q=new int[row*col];
    cout<<"Enter first matrix : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
           cin>>p[i*col+j];
    cout<<"Enter Second matrix : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
           cin>>q[i*col+j];
    cout<<"Difference of two matrix "<<endl;
    for(int i=0;i<row;i++)
    {
       for(int j=0;j<col;j++)
          cout<<p[i*col+j]-q[i*col+j]<<" ";
       cout<<endl;
    }
    return 0;
}



C++ program to subtract one matrix from another matrix using function

#include<iostream>
using namespace std;
void sub(int (&)[10][10],int (&)[10][10],int,int);
int main()
{
    int row,col,a[10][10],b[10][10];
    cout<<"Enter rows and columns of matrices : ";
    cin>>row>>col;
    cout<<"Enter elements of first matrix : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            cin>>a[i][j];
    cout<<"Enter elements of second matrix : "<<endl;
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            cin>>b[i][j];
    sub(a,b,row,col);
    cout<<"Difference of two matrices : "<<endl;
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
             cout<<a[i][j]<<" ";
            cout<<endl;
    }
    return 0;
}
void sub(int (&a)[10][10],int (&b)[10][10],int row,int col)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            a[i][j]+=b[i][j];
}



C++ to subtract two matrices using operator overloading

#include<iostream>
using namespace std;
struct sub
{
    int a[3][3];
};
sub operator-(sub mat1,sub mat2)
{
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            mat1.a[i][j]-=mat2.a[i][j];
    return mat1;
}
int main()
{
    sub mat1,mat2,mat3;
    cout<<"Enter elements of first matrix : "<<endl;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            cin>>mat1.a[i][j];
    cout<<"Enter elements of second matrix : "<<endl;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            cin>>mat2.a[i][j];
    mat3=mat1-mat2;
    cout<<"Subtraction of two matrices : "<<endl;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
             cout<<mat3.a[i][j]<<" ";
            cout<<endl;
    }
    return 0;
}

No comments:

Post a Comment