Skip to main content
3 of 6
added 859 characters in body

Numerical algebra using a Matrix class

Using Vector, is it okey?

#include <iostream>
#include <vector>

using namespace std;

class Matrix {

private:
vector< vector<double> > array;
int row, column;

public:

Matrix(int m, int n) {
    row = m;
    column = n;
    array = vector< vector<double> > (m, vector<double> (n, 0));
}


Matrix(Matrix &matrix){
    this->array=matrix.array;
    this->row=matrix.row;
    this->column=matrix.column;
    
}
void print(){
    for(int i=0;i<this->row;++i){
        for(int j=0;j<this->column;++j)
            printf("%f\t",this->array[i][j]);
        printf("\n");
    }
    
 }
 vector<double> & operator [](int m){
     return this->array[m];
 }
 };

            

I would like to make a Matrix class in cpp. Can any of you help me about this class?

 #include <iostream>
 using namespace std;

 class matrix
 {
  int **p, m, n;
public:
   matrix(int row, int col)
{
    m = row;
    n = col;
    p = new int*[m];
    for (int i = 0; i < m; ++i)
    {
        p[i] = new int[n];
        for (int j = 0; j < n; ++j)
            p[i][j] = 0;
    }
}

    ~matrix()
    {
        for(int i=0;i<m;++i)
            delete [] p[i];
        delete [] p;
        
    }

void accept()
{
    cout<<"Enter matrix elements: ";
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> p[i][j];
        }
    }
}

void display()
{
    cout <<"The matrix is:\n";
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cout << p[i][j] <<" ";
        }
        cout <<endl;
    }
}

matrix& operator+ (const matrix& m1)
{
    return (*this += m1);
}

matrix& operator* (const matrix& m1)
{
    return (*this *= m1);
}

matrix& operator+= (const matrix& rhs)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            p[i][j] += rhs.p[i][j];
        }
    }
    return *this;
}

matrix& operator*= (const matrix& T)
{
    if(n == T.m)
    {
        for(int i = 0; i < T.m; ++i)
        {
            for(int k = 0; k < n; ++k)
            {
                p[i][k] *= T.p[k][i];
            }
        }
    }
    
    return *this;
  }

  matrix& operator= (const matrix& T)
  {
    p = T.p;
    n = T.n;
    m = T.m;
    
    return *this;
}

};


 int main()
  {
matrix rf(3,3), ff(3,3);
rf.accept();
cout << "\nrf\n";
rf.display();

ff.accept();
cout << "\nff\n";
ff.display();

cout << "\nff = ff + rf\n";
ff = ff + rf;
ff.display();

cout << "\nff = (rf * ff)\n";
ff = (rf * ff);
ff.display();

cout << "\nrf += ff\n";
rf += ff;
rf.display();
 }