Skip to main content
deleted 58 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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

There are two way, and they goes as follows.ways:


 

The other one using Vector.vector:

#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? There are two way, and they goes as follows.


 

The other one using Vector.

#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. Can any of you help me about this class?

There are two ways:

The other one using vector:

#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 make the question concrete!
Source Link

There are two way, I would like to make a difference, isMatrix class in cpp. Can any of you help me about this okeyclass? There are two way, and they goes as follows.

UsingThe other one 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();
 }

There are two way, I make a difference, is this okey?

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();
 }

I would like to make a Matrix class in cpp. Can any of you help me about this class? There are two way, and they goes as follows.

The other one using Vector.

#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 fix a problem ,am I?
Source Link

There are two way, I make a difference, is this okey?

#include <iostream>

class Matrix {
    
private:
    double** array;
    int row, column;
    
public:

    Matrix(int m, int n) {
        this->row=m;
        this->column=n;
        this->array = new double*[m];
        for (int i = 0; i < n; i++){
            this->array[i] = new double[n];
            std::fill(array[i],array[i]+n,0.0);
        }
    }

    Matrix(Matrix &matrix){
        this->array=matrix.array;
        this->row=matrix.row;
        this->column=matrix.column;
        
    }
    Matrix& operator=(const Matrix& rhs ){
        this->row=rhs.row;
        this->column=rhs.column;
        for(int i=0;i<row;++i)
            for(int j=0;j<column;++j)
                this->array[i][j]=rhs.array[i][j];
    }

    ~Matrix() {
        for (int i = 0; i < this->row; i++)
            delete[] this->array[i];
        delete[] this->array;
    }

   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");
        }
        
    }
    double * operator [](int m){
        return this->array[m];
    }
};
int main(int argc, const char * argv[]) {
    Matrix A(2,2);
    A[0][1]=1;
    A[0][0]=0;
    A[1][0]=1;
    A[1][1]=1;
    A.print();
    return 0;
  }

Using Vector, is it okey?

Using Vector, is it okey?

There are two way, I make a difference, is this okey?

#include <iostream>

class Matrix {
    
private:
    double** array;
    int row, column;
    
public:

    Matrix(int m, int n) {
        this->row=m;
        this->column=n;
        this->array = new double*[m];
        for (int i = 0; i < n; i++){
            this->array[i] = new double[n];
            std::fill(array[i],array[i]+n,0.0);
        }
    }

    Matrix(Matrix &matrix){
        this->array=matrix.array;
        this->row=matrix.row;
        this->column=matrix.column;
        
    }
    Matrix& operator=(const Matrix& rhs ){
        this->row=rhs.row;
        this->column=rhs.column;
        for(int i=0;i<row;++i)
            for(int j=0;j<column;++j)
                this->array[i][j]=rhs.array[i][j];
    }

    ~Matrix() {
        for (int i = 0; i < this->row; i++)
            delete[] this->array[i];
        delete[] this->array;
    }

   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");
        }
        
    }
    double * operator [](int m){
        return this->array[m];
    }
};
int main(int argc, const char * argv[]) {
    Matrix A(2,2);
    A[0][1]=1;
    A[0][0]=0;
    A[1][0]=1;
    A[1][1]=1;
    A.print();
    return 0;
  }

Using Vector, is it okey?

added 859 characters in body
Source Link
Loading
deleted 28 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Post Closed as "Needs details or clarity" by forsvarir, Mast, 301_Moved_Permanently, Jamal
Source Link
Loading