对一个固定的3×3矩阵进行转置
#include <iostream>
void matrixT(int src[3][3],int dest[3][3]) {
for (int i=0;i<3;++i){
for (int j=0;j<3;++j){
dest[j][i]=src[i][j];
}
}
}
int main(){
int raw_matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int Tmatrix[3][3]={0};
std::cout<<"raw matrix:\n";
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << raw_matrix[i][j] << " ";
}
std::cout << std::endl;
}
matrixT(raw_matrix,Tmatrix) ;
// 打印转置后的矩阵
std::cout << "Transposed matrix:\n";
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << Tmatrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}