Finding the transpose of a matrix is a common task in C programming. You've got a 2x3 matrix? Its transpose will end up being a 3x2 matrix, where rows become columns. Let’s check out how to implement this in C.
Prerequisites for Finding the Transpose in C
Here’s what you need:
- Understanding of 2D arrays
- Loops in C (for loop)
- Basic I/O functions:
scanfandprintf - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Find Transpose of a Matrix
Here's the complete code to find the transpose of a given matrix.
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], row, col, i, j;
// Read rows and columns
printf("Enter the number of rows and columns: ");
scanf("%d %d", &row, &col);
// Read matrix elements
printf("Enter elements of the matrix:\n");
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) {
printf("Enter element a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
// Transpose the matrix
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) {
transpose[j][i] = a[i][j];
}
}
// Output the transposed matrix
printf("Transpose of the matrix:\n");
for (i = 0; i < col; ++i) {
for (j = 0; j < row; ++j) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Transpose of a Matrix Program
Here's what you see when you run the program. We entered a 2x3 matrix.
Enter the number of rows and columns: 2 3
Enter elements of the matrix:
Enter element a[0][0]: 1
Enter element a[0][1]: 2
Enter element a[0][2]: 3
Enter element a[1][0]: 4
Enter element a[1][1]: 5
Enter element a[1][2]: 6
Transpose of the matrix:
1 4
2 5
3 6 
Algorithm to Transpose a Matrix in C
The algorithm to find the transpose of a matrix involves rearranging the rows and columns. Here's a step-by-step guide to how the program works.
- Read the number of rows and columns from the user.
- Input the matrix elements from the user.
- Initialize the transpose matrix.
- For each element, set
transpose[j][i] = matrix[i][j]. - Output the transpose matrix.
flowchart TD
A([Start]) --> B[/Read rows and columns/]
B --> C[/Read matrix elements/]
C --> D[Initialize transpose matrix]
D --> E{More elements?}
E -->|Yes| F["transpose[j][i] = matrix[i][j]"]
F --> G[Increment indices]
G --> E
E -->|No| H[/Output transpose matrix/]
H --> I([End])How the Transpose Program Works
Let's break down the code line by line to see how it achieves the transpose of a matrix.
- The program starts with the
#include <stdio.h>directive, which allows the use of input/output functions. - Declare a 2D array
aand another arraytransposeto store the transposed matrix. - Use
printfto prompt the user to enter the number of rows and columns, and read them usingscanf. - Loops through each element of the matrix to input its values using nested
forloops. - Nested loops again to compute the transpose, swapping
iandjindices. - Finally, output the transposed matrix using
printfwithin nested loops. - The program ends with
return 0, indicating successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the transpose of a matrix involve?
Time and Space Complexity of the Transpose Program
Here's the complexity of the transpose operation.
Time Complexity: O(n * m), where n is the number of rows and m is the number of columns. This is because the program visits each element of the matrix once with the loop for (i = 0; i < row; ++i) running fully for each column. For example, with a 3x3 matrix, that means visiting 9 elements.
Space Complexity: O(n * m) since an additional matrix transpose of the same size as the original is used to store the result. Handles as many elements as a, so for a 2x3 matrix, it involves storing 6 elements.
Common mistake: Not swapping the indices i and j when copying elements into the transpose matrix leads to wrong transpositions.
Key takeaway: The transpose operation switches rows and columns, rearranging the whole matrix.
FAQs About Finding Transpose of a Matrix in C
Here are some common questions about transposing matrices in C.
matrix[i][j] to transpose[j][i].Related Programs
If you found this program helpful, check out these related programs:
- C Program to Multiply Two Matrices Using Multi-dimensional Arrays
- C Program to Add Two Matrices Using Multi-dimensional Arrays
- C Program to Find Largest Element in an Array
- C Program to Multiply Two Matrices Using Functions
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
Transpose is a basic operation in matrix manipulation. It's super useful in data analysis and mathematical transformations. Go ahead and experiment with different matrix sizes to see how it changes the output structure.
Practice more with our C programming quizzes or try running this and other codes in the EduSeekho online compiler.