matrixtranspose.cpp

来自「这是数据结构、算法与应用-C++语言描述的代码」· C++ 代码 · 共 33 行

CPP
33
字号
// in-place matrix transpose

#include <iostream>
#include "make2dArrayNoCatch.h"

using namespace std;

template<class T>
void transpose(T **a, int rows)
{// In-place transpose of matrix a[0:rows-1][0:rows-1].
   for (int i = 0; i < rows; i++)
      for (int j = i+1;  j < rows; j++)
         swap(a[i][j], a[j][i]);
}

int main()
{
   int **a;
   make2dArray(a,2,2);
   a[0][0] = 1; a[0][1] = 2; a[1][0] = 3; a[1][1] = 4;

   cout << "The matrix is" << endl;
   cout << a[0][0] << ' ' << a[0][1] << endl;
   cout << a[1][0] << ' ' << a[1][1] << endl;

   transpose(a,2);

   cout << "The transposed matrix is" << endl;
   cout << a[0][0] << ' ' << a[0][1] << endl;
   cout << a[1][0] << ' ' << a[1][1] << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?