行列の転置を行うプログラム

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 の 2 つの行列 mat1mat2 を宣言します。
  2. coutcin を使用して、ユーザーに行列の要素を入力してもらいます。
  3. cout を使用して元の行列 mat1 を表示します。
#include <iostream>

using namespace std;

int main()
{
    // 行列の初期化
    int mat1[3][3], mat2[3][3];
    int i, j;

    // ユーザーによる要素の入力
    cout << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 元の行列の表示
    cout << "\nMatrix is: " << 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 << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 元の行列の表示
    cout << "\nMatrix is: " << 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 << "\nTransposed matrix is: " << 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 << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // 元の行列の表示
    cout << "\nMatrix is: " << 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 << "\nTransposed matrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat2[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

まとめ

この実験では、C++ で行列の転置を行うプロセスを案内しました。与えられた手順に従うことで、行列を初期化して転置する方法をより深く理解していただけるはずです。行列の転置は線形代数における重要な演算であり、C++ で行うことで様々な計算タスクに役立ちます。