Answer the question
In order to leave comments, you need to log in
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
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; //ошибка
}
};
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;
}
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question