2

I want to create a transpose function for dynamic 2d arrays. I want the functions to have as parameters the 2d array and the rows and columns. I ve decided to use double pointer. However i m a bit confused about how i gonna call the function from main. So i ve got the above code

#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?

snake plissken
  • 2,649
  • 10
  • 43
  • 64
  • 1
    Please try to format the code in your questions. Astyle is an example of a good code formater. Also, is this homework? –  Dec 18 '11 at 01:54
  • 1
    This code has a logic error in it. `mat = new double*[rows];` is going to cause you to lose your passed in array. ` –  Dec 18 '11 at 02:00
  • It is not a homework, actually i want to create a function that transpose general 2d functions for a project. The trasnpose function works correctly. If i put the matrix initialization inside function it returns the desired results. But i dont know how to call it. – snake plissken Dec 18 '11 at 02:28
  • Mmm, this code transposes the matrix in-place. It works only if the matrix is square, so rows = columns. – nat chouf Feb 19 '13 at 22:34

3 Answers3

5

There are a couple of major issues with your code.

The biggest one is that a double[10][10] is not convertible to a double** pointer.

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

I recommend that you separate the concerns of printing a matrix and transposing a matrix. Perhaps a separate methods on a (templated) matrix class.

And now, having said that...


Why write one when a perfectly good 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))
Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • How can i use trans in an array? Not in matrix.hpp. This code was just an example of what i want to do. I want to create a function which purpose to transpose matrices tha already exists. – snake plissken Dec 18 '11 at 02:30
  • If this is homework (it's stated that it's not), then this is unlikely to help. However, this answer would help a C++ developer looking to do matrix transposition. – johnsyweb Dec 18 '11 at 02:34
5

You're very close. You're calling the function correctly and the function's parameter list 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 up. (There was one missing.) You can't define a static array (one that looks like this: x[y][z]) with non-constant variables as the size arguments. (I.e. y and z must be constants.) But actually, you're passing a dynamic array to the transpose function anyway, and rows and columns don't have to be constants to do that. So, in main, define a dynamic array like this:

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 could also make it better by putting your matrix display code in a function. Then, instead of cutting and pasting it everywhere, all you have to do is call the function! It's an important habit to get into. Have fun!

David Winiecki
  • 4,093
  • 2
  • 37
  • 39
2
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 transpose of the matrix.

Amna
  • 21
  • 2