The transpose of a matrix is obtained by interchanging its rows and columns. In other words, if the original matrix is denoted by A, then its transpose, denoted by AT, is given by:
AT[i][j] = A[j][i]
Here's an example of a function in C that takes a matrix and its dimensions as input, and returns its transpose:
Here's an example of a program in C to find the transpose of a matrix:
#include
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS];
int i, j;
printf("Enter matrix elements:\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal matrix:\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\nTransposed matrix:\n");
for (i = 0; i < COLS; i++) {
for (j = 0; j < ROWS; j++) {
printf("%d ", matrix[j][i]);
}
printf("\n");
}
return 0;
}
The program first defines a matrix with a fixed number of rows and columns. It then asks the user to enter the matrix elements using nested loops. After that, it prints the original matrix using another set of nested loops.
Next, the program calculates the transpose of the matrix by iterating over the rows and columns in reverse order, and prints the transposed matrix using another set of nested loops.
The time complexity of the program is O(n^2), where n is the number of rows or columns in the matrix.