C++ Online Compiler
Example: Find Transpose of a Matrix in C++ language using Array
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Find Transpose of a Matrix in C++ language using Array #include <iostream> using namespace std; int main() { int r, c, i, j; // r - denotes the matrix's rows // c - dentes the matrix's columns cout << "-----Enter the number of rows & columns of the matrix-----\n"; cin >> r >> c; int a[r][c], t[c][r]; // a - denotes the input matrix // t - denotes the transpose matrix cout << "\n-----Enter the matrix's elements-----\n"; for (i = 0; i < r; ++i) { for (j = 0; j < c; ++j) { cout << "Enter element at position[" << i + 1 << j + 1 << "]: "; cin >> a[i][j]; } cout << "\n"; } // It will display the input matrix cout << "\n-----The entered matrix-----\n"; for (i = 0; i < r; ++i) { cout << "\t"; for (j = 0; j < c; ++j) { cout << a[i][j] << "\t"; if (j == c - 1) cout << "\n"; } cout << "\n"; } // It will transposes matrix for (i = 0; i < r; ++i) { for (j = 0; j < c; ++j) { t[j][i] = a[i][j]; } } // It will display the transposed matrix cout << "\n-----The transpose of the matrix-----\n"; for (i = 0; i < c; ++i) { cout << "\t"; for (j = 0; j < r; ++j) { cout << t[i][j] << "\t"; if (j == r - 1) cout << "\n"; } cout << "\n"; } return 0; }
3 4 23 43 53 23 3 4 5 6 23 65 76 34
Output
Clear
ADVERTISEMENTS