C Online Compiler
Example: Find Transpose of a Matrix in C language using Array
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Transpose of a Matrix in C language using Array #include <stdio.h> int main() { int r, c, i, j; // r - denotes the matrix's rows // c - dentes the matrix's columns printf("-----Enter the number of rows & columns of the matrix-----\n"); scanf("%d %d", &r, &c); int a[r][c], t[c][r]; // a - denotes the input matrix // t - denotes the transpose matrix printf("\n-----Enter the matrix's elements-----\n"); for (i = 0; i < r; ++i) { for (j = 0; j < c; ++j) { printf("Enter element at position[%d%d]: ", i + 1, j + 1); scanf("%d", &a[i][j]); } printf("\n"); } // It will display the input matrix printf("\n-----The entered matrix-----\n"); for (i = 0; i < r; ++i) { printf("\t"); for (j = 0; j < c; ++j) { printf("%d\t", a[i][j]); if (j == c - 1) printf("\n"); } printf("\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 printf("\n-----The transpose of the matrix-----\n"); for (i = 0; i < c; ++i) { printf("\t"); for (j = 0; j < r; ++j) { printf("%d\t", t[i][j]); if (j == r - 1) printf("\n"); } printf("\n"); } return 0; }
3 4 12 34 56 23 67 32 89 23 12 23 99 23
Output
Clear
ADVERTISEMENTS