2

我想为动态二维数组创建一个转置函数。我希望函数将二维数组以及行和列作为参数。我决定使用双指针。然而,我对如何从 main 调用函数有点困惑。所以我得到了上面的代码

#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;
}

任何想法?

4

3 回答 3

5

你很亲密。您正确调用了该函数并且该函数的参数列表是正确的。首先,从转置函数中删除这一部分:

 mat = new double*[rows];

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

 }

现在确保所有括号都匹配。(缺少一个。)您不能x[y][z]使用非常量变量作为大小参数来定义静态数组(看起来像这样:)。(即y并且z必须是常量。)但实际上,无论如何,您都在将动态数组传递给转置函数,rows并且columns不必是常量即可。因此,主要定义一个动态数组,如下所示:

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

之后,您的代码应该可以工作。但是您也可以通过将矩阵显示代码放入函数中来使其变得更好。然后,无需到处剪切和粘贴,您所要做的就是调用该函数!这是一个重要的习惯。玩得开心!

于 2011-12-18T02:34:21.717 回答
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))
于 2011-12-18T02:18:14.570 回答
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;
}

这是矩阵转置的代码。

于 2015-02-12T21:15:54.087 回答