Matrix

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.9 kB
4
Indexable
Never
#include <iostream>

using namespace std;
class matrix{
  int a[3][3],b[3][3],final[3][3],i,j,k;
  public:
  void getData();
  void dispData();
  void add();
  void sub();
  void multiply();
};

void matrix::getData(){
  int i,j,k;
  cout<<"Enter the element of first matrix\n";
  for (i=0;i<3;i++){
        for (j=0;j<3;j++){
            //cout<<"Enter the "<<i<<"element";
            cin>>a[i][j];
        }

  }
  cout<<"\nEnter the element of second matrix\n";
  for (i=0;i<3;i++){
        for (j=0;j<3;j++){
            //cout<<"Enter the "<<i<<"element";
            cin>>b[i][j];
        }

  }
}

void matrix::dispData(){
  int i,j;
  cout<<"The first Matrix is: ";
  for (i=0;i<3;i++){
        cout<<"\n";
        for (j=0;j<3;j++){
            cout<<a[i][j]<<" ";
        }

  }
    cout<<"\n\nThe second Matrix is: ";
  for (i=0;i<3;i++){
        cout<<"\n";
        for (j=0;j<3;j++){
            cout<<b[i][j]<<" ";
        }

  }
}

void matrix::add(){
  cout<<"\n\nAddition of matrix is: ";
  for (i=0;i<3;i++){
        cout<<"\n";
        for (j=0;j<3;j++){
            final[i][j]=a[i][j]+b[i][j];
            cout<<final[i][j]<<" ";
        }

  }
}

void matrix::sub(){
  cout<<"\n\nSubtraction of matrix is: ";
  for (i=0;i<3;i++){
        cout<<"\n";
        for (j=0;j<3;j++){
            final[i][j]=a[i][j]-b[i][j];
            cout<<final[i][j]<<" ";
        }

  }
}

void matrix::multiply(){
  cout<<"\n\nMultiplication of matrix is: ";
  for (i=0;i<3;i++){
        for (j=0;j<3;j++){
            for (k=0;k<3;k++){
                final[i][j]+=a[i][k]*b[k][j];
            }
        }

  }
   for (i=0;i<3;i++){
        cout<<"\n";
        for (j=0;j<3;j++){
            cout<<final[i][j]<<" ";
        }

  }
}

int main()
{
    matrix m;
    m.getData();
    m.dispData();
    m.add();
    m.sub();
    m.multiply();
    return 0;
}