实现矩阵转置的程序

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

本实验将引导你完成在 C++ 中执行矩阵转置的过程。矩阵的转置是通过交换矩阵的行和列来获得的,结果是一个新的矩阵,其中列变为行,行变为列。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") subgraph Lab Skills cpp/arrays -.-> lab-96241{{"`实现矩阵转置的程序`"}} cpp/for_loop -.-> lab-96241{{"`实现矩阵转置的程序`"}} cpp/output -.-> lab-96241{{"`实现矩阵转置的程序`"}} cpp/user_input -.-> lab-96241{{"`实现矩阵转置的程序`"}} end

初始化矩阵

  1. 声明两个 3x3 矩阵,mat1mat2
  2. 使用 coutcin 让用户输入矩阵的元素。
  3. 使用 cout 显示原始矩阵 mat1
#include <iostream>

using namespace std;

int main()
{
    // 初始化矩阵
    int mat1[3][3], mat2[3][3];
    int i, j;

    // 获取用户输入的元素
    cout << "输入矩阵 (3x3) 的元素: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 显示原始矩阵
    cout << "\n矩阵为: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

矩阵转置

  1. 通过交换原始矩阵的行和列来执行矩阵的转置。
  2. 将转置后的矩阵结果存储在 mat2 中。
#include <iostream>

using namespace std;

int main()
{
    // 初始化矩阵
    int mat1[3][3], mat2[3][3];
    int i, j;

    // 获取用户输入的元素
    cout << "输入矩阵 (3x3) 的元素: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 显示原始矩阵
    cout << "\n矩阵为: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    // 矩阵转置
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            mat2[i][j] = mat1[j][i];
        }
    }

    // 显示转置后的矩阵
    cout << "\n转置后的矩阵为: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat2[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

完整代码

#include <iostream>

using namespace std;

int main()
{
    // 初始化矩阵
    int mat1[3][3], mat2[3][3];
    int i, j;

    // 获取用户输入的元素
    cout << "输入矩阵 (3x3) 的元素: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 显示原始矩阵
    cout << "\n矩阵为: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    // 矩阵转置
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            mat2[i][j] = mat1[j][i];
        }
    }

    // 显示转置后的矩阵
    cout << "\n转置后的矩阵为: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat2[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

总结

本实验引导你完成了在 C++ 中执行矩阵转置的过程。通过遵循给定的步骤,你现在应该对如何初始化和转置矩阵有了更好的理解。矩阵转置是线性代数中的一项重要操作,在 C++ 中执行它可以帮助你完成各种计算任务。

您可能感兴趣的其他 C++ 教程