原文: https://www.programiz.com/cpp-programming/examples/matrix-transpose
该程序从用户处获取阶数为r * c的矩阵,并计算该矩阵的转置。
要理解此示例,您应该了解以下 C++ 编程主题:
在此程序中,要求用户输入行数和列数。 在此程序中,row和column的值应小于 10。
然后,要求用户输入矩阵的元素。
程序将计算矩阵的转置并将其显示在屏幕上。
示例:查找矩阵的转置
#include <iostream>using namespace std;int main() {int a[10][10], transpose[10][10], row, column, i, j;cout << "Enter rows and columns of matrix: ";cin >> row >> column;cout << "\nEnter elements of matrix: " << endl;// Storing matrix elementsfor (int i = 0; i < row; ++i) {for (int j = 0; j < column; ++j) {cout << "Enter element a" << i + 1 << j + 1 << ": ";cin >> a[i][j];}}// Printing the a matrixcout << "\nEntered Matrix: " << endl;for (int i = 0; i < row; ++i) {for (int j = 0; j < column; ++j) {cout << " " << a[i][j];if (j == column - 1)cout << endl << endl;}}// Computing transpose of the matrixfor (int i = 0; i < row; ++i)for (int j = 0; j < column; ++j) {transpose[j][i] = a[i][j];}// Printing the transposecout << "\nTranspose of Matrix: " << endl;for (int i = 0; i < column; ++i)for (int j = 0; j < row; ++j) {cout << " " << transpose[i][j];if (j == row - 1)cout << endl << endl;}return 0;}
输出
Enter rows and columns of matrix: 23Enter elements of matrix:Enter element a11: 1Enter element a12: 2Enter element a13: 9Enter element a21: 0Enter element a22: 4Enter element a23: 7Entered Matrix:1 2 90 4 7Transpose of Matrix:1 02 49 7
