Transpose 2d dynamic matrix

I want to create a transpose function for 2d dynamic arrays. I want the functions to have a 2d array as parameters, as well as rows and columns. I decided to use a double pointer. However, I am a little confused about how I call functions from the main one. So I got the code above

#include<iostream> using namespace std; void transposeMatrix(double **mat, int rows, int columns) { mat = new double*[rows]; for (int i = 0; i < rows; ++i) { mat[i] = new double[columns]; } double temp; for (int i = 0; i<rows; i++) { for (int j = i+1; j<columns; j++) { temp=mat[i][j]; mat[i][j]=mat[j][i]; mat[j][i]=temp; } } cout<< "\n"; for (int i = 0; i<rows; i++) { for (int j = 0; j<columns; j++) { cout << mat[i][j] << " \t"; } cout << "\n"; } } int main() { int rows = 10; int columns = 10; double mat[rows][columns]; for (int i = 0; i<rows; i++) { for (int j = 0; j<columns; j++) { mat[i][j] = j; } } for (int i = 0; i<rows; i++) { for (int j = 0; j<columns; j++) { cout << mat[i][j] << " \t"; } cout << "\n"; } //mat = new double[50][1]; transposeMatrix(mat, 10, 10); system("pause"); return 0; } 

Any idea?

+4
source share
3 answers

You are very close. You call the function correctly, and the list of function parameters is correct. First remove this section from the transpose function:

  mat = new double*[rows]; for (int i = 0; i < rows; ++i) mat[i] = new double[columns]; } 

Now make sure all your brackets match. (There was one missing.) You cannot define a static array (which looks like this: x[y][z] ) with mutable variables as size arguments. (Ie y and z must be constants.) But you are actually passing a dynamic array to the transpose function anyway, and rows and columns should not be constants for this. So basically, define a dynamic array as follows:

 double** mat = new double*[rows]; for (int i = 0; i < rows; i++) mat[i] = new double[columns]; 

After that, your code should work. But you can also do it better by putting your matrix display code into a function. Then, instead of cutting and pasting it everywhere, all you have to do is call the function! This is an important habit. Enjoy!

+5
source

There are several serious issues in your code.

The largest of them: double[10][10] not convertible to pointer double** .

You also have a memory leak ( mat ) in your transposeMatrix() implementation.

I recommend separating matrix printing and matrix transfer issues. Perhaps separate methods on a (template) matrix class.

And now, saying that ...


Why write when an excellent implementation already exists ?

Example:

 #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp> int main () { using namespace boost::numeric::ublas; matrix<double> m(3, 3); for (unsigned i = 0; i < m.size1(); ++i) { for (unsigned j = 0; j < m.size2(); ++j) { m(i, j) = 3 * i + j; } } std::cout << m << std::endl; std::cout << trans(m) << std::endl; } 

Output:

 [3,3]((0,1,2),(3,4,5),(6,7,8)) [3,3]((0,3,6),(1,4,7),(2,5,8)) 
+5
source
 double ** transpose(double **matrix, int rows, int columns){ double ** trans; trans=new double *[columns]; for(int i=0;i<columns;i++){ trans[i]=new double[rows]; for(int j=0;j<rows;j++) trans[i][j]=matrix[j][i]; } return trans; for(int i=0;i<columns;i++) delete[] trans[i]; delete[] trans; } 

Here is the code for transposing the matrix.

+2
source

Source: https://habr.com/ru/post/1386886/


All Articles