阅读量:85
你可以通过以下代码实现C++矩阵的转置:
#include
const int rows = 3;
const int cols = 3;
void transposeMatrix(int matrix[rows][cols], int result[cols][rows]) {
for (int i = 0; i < rows class="hljs-keyword">for (int j = 0; j < cols xss=removed class="hljs-function">int main() {
int matrix[rows][cols] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int result[cols][rows];
transposeMatrix(matrix, result);
std::cout << "Original Matrix:" << std class="hljs-keyword">for (int i = 0; i < rows class="hljs-keyword">for (int j = 0; j < cols class="hljs-string">" ";
}
std::cout << std class="hljs-string">"Transposed Matrix:" << std class="hljs-keyword">for (int i = 0; i < cols class="hljs-keyword">for (int j = 0; j < rows class="hljs-string">" ";
}
std::cout << std class="hljs-keyword">return 0;
}
在上面的代码中,transposeMatrix函数用于计算矩阵的转置,并将结果存储在result数组中。在main函数中,我们定义了一个3x3的矩阵matrix,并调用transposeMatrix函数来计算转置矩阵,并输出原始矩阵和转置矩阵。