Transposing a matrix in C involves swapping the rows and columns of a given matrix to create a new matrix. The element at position (i,j)(i, j) in the original matrix becomes the element at position (j,i)(j, i) in the transposed matrix. Here's a detailed explanation along with a sample implementation.

Explanation of Transpose of a Matrix

  1. Definition:

    • The transpose of a matrix is formed by turning its rows into columns and its columns into rows. For example, if you have a matrix AA of size m×nm \times n, its transpose ATA^T will have the size n×mn \times m.
  2. Mathematical Representation:

    • If A[i][j]A[i][j] is the element of the original matrix, the transposed matrix B[j][i]B[j][i] is defined as:
    B[j][i]=A[i][j]B[j][i] = A[i][j]
  3. Conditions:

    • The transpose operation can be performed on any rectangular or square matrix.

Sample C Program for Transposing a Matrix

Here is a simple C program that demonstrates how to compute the transpose of a matrix:

#include <stdio.h> #define MAX 10 // Maximum size of the matrix void inputMatrix(int matrix[MAX][MAX], int rows, int cols) { printf("Enter elements of the matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Element [%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } } void printMatrix(int matrix[MAX][MAX], int rows, int cols) { printf("The matrix is:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } void transposeMatrix(int matrix[MAX][MAX], int transposed[MAX][MAX], int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed[j][i] = matrix[i][j]; // Swap rows and columns } } } int main() { int A[MAX][MAX], B[MAX][MAX]; int rows, cols; // Input dimensions for the matrix printf("Enter number of rows and columns for the matrix: "); scanf("%d %d", &rows, &cols); // Input the matrix printf("Matrix A:\n"); inputMatrix(A, rows, cols); // Transpose the matrix transposeMatrix(A, B, rows, cols); // Print the original and transposed matrix printf("Original Matrix A:\n"); printMatrix(A, rows, cols); printf("Transposed Matrix B:\n"); printMatrix(B, cols, rows); // Note: Rows and cols are swapped for transposed matrix return 0; }

Explanation of the Code

  1. Header Files:

    • #include <stdio.h>: Includes the standard input-output library.
  2. Macro Definition:

    • #define MAX 10: Defines a constant for the maximum size of the matrices.
  3. Functions:

    • inputMatrix: This function prompts the user to enter the elements of the matrix.
    • printMatrix: This function prints the matrix to the console.
    • transposeMatrix: This function computes the transpose of the given matrix by swapping rows and columns.
  4. main Function:

    • Prompts the user for the dimensions of the matrix.
    • Calls inputMatrix to fill the original matrix AA.
    • Calls transposeMatrix to compute the transposed matrix BB.
    • Calls printMatrix to display both the original and transposed matrices.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc transpose_matrix.c -o transpose_matrix
  2. Execute the Program:

    ./transpose_matrix
  3. Input Data: Follow the prompts to input the dimensions and elements of the matrix.

Example Input/Output

Input:

Enter number of rows and columns for the matrix: 2 3 Matrix A: Element [0][0]: 1 Element [0][1]: 2 Element [0][2]: 3 Element [1][0]: 4 Element [1][1]: 5 Element [1][2]: 6

Output:

Original Matrix A: 1 2 3 4 5 6 Transposed Matrix B: 1 4 2 5 3 6

Conclusion

Transposing a matrix is a fundamental operation in various fields such as linear algebra, computer graphics, and machine learning. The above example provides a clear understanding of how to implement matrix transposition in C. You can extend this program further by adding error handling, supporting larger matrices, or using dynamic memory allocation.