A
A
Alexander Sharomet2014-11-18 14:06:31
Programming
Alexander Sharomet, 2014-11-18 14:06:31

How to return a two dimensional array in c++?

How can you return such an array?

class Matrix{
  public:
    int i,j;
    int returnMatrix(int col,int row){
        int matrix[col][row];
        for(i=0;i<row;i++){
            matrix[i][j]=i;
            for(j=0;j<col;j++){
                matrix[i][j]=j;
            }
        }
        return matrix[][]; //ошибка

    }
};

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2014-11-18
@sharomet

class Matrix{
  public:
    int i,j;
    int[][] returnMatrix(int col,int row){
        int matrix[col][row];
        for(i=0;i<row;i++){
            matrix[i][j]=i;
            for(j=0;j<col;j++){
                matrix[i][j]=j;
            }
        }
        return matrix; //ошибка

    }
};

S
Sergey Mishin, 2014-11-18
@sergeysmishin

class Matrix{
private:
    int **  _matrix;
    int _col;
    int _row;

public:
    Matrix(int col, int row)
    {
        _col = col;
        _row = row;
        
        _matrix = new int*[_row];
        for(int i=0;i<_row;i++)
        {
            _matrix[i] = new int[_col];
        }
    }

    int** ReturnMatrix()
    {
        int i,j;
        for(i = 0; i < _row; i++)
        {
            for(j = 0; j < _col; j++)
            {
                _matrix[i][j]=j;
            }
        }
        return _matrix; 
    }

    ~Matrix()
    {
        for(int i=0; i<_row; i++)
        {
            delete[] _matrix[i];
        }
        delete[] _matrix;
    }
};

int main()
{
    Matrix* m = new Matrix(2,2);
    int** matrix = m->ReturnMatrix();

    for(int i=0;i<2;i++)
    {
            for(int j=0;j<2;j++)
            {
                cout << matrix[i][j];
            }
            cout << '\n';
    }
    delete m;
    return 0;
}

But it's better like this:
class Matrix{
private:
    int **  _matrix;
    int _col;
    int _row;

public:
    Matrix(int col, int row)
    {
        _col = col;
        _row = row;
        _matrix = new int*[_row];
        for(int i=0;i<_row;i++)
        {
            _matrix[i] = new int[_col];
        }
    }

    void MakeMatrix(){
        int i,j;
        for(i = 0; i < _row; i++)
        {
            for(j = 0; j < _col; j++)
            {
                _matrix[i][j]=j;
            }
        }
    }     

    int GetValue(int i, int j)
    {
        return _matrix[i][j];
    }

    ~Matrix()
    {
        for(int i=0; i<_row; i++)
        {
            delete[] _matrix[i];
        }
        delete[] _matrix;
    }
};

int main()
{
    Matrix* m = new Matrix(2,2);
    m->MakeMatrix();

    for(int i=0;i<2;i++)
    {
            for(int j=0;j<2;j++)
            {
                cout << m->GetValue(i,j);
            }
            cout << '\n';
    }
    delete m;
    return 0;
}

K
Koss1024, 2014-11-18
@Koss1024

boost::ublas::matrix

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question