In this article, we provide a detailed guide to transposing a matrix in C. You’ll find a clear algorithm, the complete C program, and a step-by-step explanation of each part of the process. This guide is perfect for beginners and intermediate programmers looking to understand matrix transposition in C.
Algorithm to Transpose a Matrix
Step 1: Start
Step 2: Input: Read the number of rows and columns for the matrix.
Step 3: Input Elements: Read the elements of the matrix.
Step 4: Transpose Matrix:
(i) Create a result matrix with dimensions (columns x rows)
of the original matrix.
(ii) For each element in the original matrix, place it in the
transposed position in the result matrix.
Step 5: Output: Display the transposed matrix.
Step 6: End
C Program to Transpose a Matrix
#include <stdio.h>
int main() {
int rows, cols;
// Step 2: Input dimensions of the matrix
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
// Step 3: Declare the matrix and the transpose matrix
int matrix[rows][cols], transpose[cols][rows];
// Step 3: Input elements of the matrix
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Step 4: Transpose the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Step 5: Output the transposed matrix
printf("Transposed matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
// Step 6: End
return 0;
}
Explanation of Each Step
Include Header:
#include <stdio.h>
- This line includes the standard input-output library for using functions like printf and scanf.
Input Dimensions:
int rows, cols;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
- These lines read the number of rows and columns for the matrix from the user.
Declare Matrices and Input Elements:
int matrix[rows][cols], transpose[cols][rows];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
- These lines declare the original matrix and the transpose matrix.
- The nested loops read the elements of the matrix from the user.
Transpose the Matrix:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
- These nested loops transpose the matrix by swapping rows with columns. For each element in the original matrix, its position is swapped and placed into the transpose matrix.
Output of the Transposed Matrix:
printf("Transposed matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
- These nested loops print the transposed matrix.
End:
return 0;
- This line indicates the successful completion of the program.
This program efficiently transposes a matrix in C by swapping the rows and columns and displays the transposed matrix
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.