C Program to Find Transpose of a Matrix

Finding the transpose of a matrix is a fundamental operation in linear algebra, which is often required in various applications such as computer graphics, machine learning, and scientific computing. In this article, we will explore how to write a C Program to Find Transpose of a Matrix using three different solutions, each demonstrated with example code and outputs. The article will be structured with necessary headings for clarity and easy navigation.

Prerequisites

Before proceeding with the code, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with arrays and loops in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Simple Iterative Approach

1.1 Explanation

In this solution, we will use a nested loop to iterate through the elements of the matrix and swap rows with columns to find the transpose.

1.2 Program

C
#include <stdio.h>

int main() {
    int rows, cols;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    int matrix[rows][cols], transpose[cols][rows];

    // Input 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]);
        }
    }

    // Find the transpose
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print the transpose
    printf("Transpose of the matrix:\n");
    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;
}

1.3 Output

C
Enter the number of rows: 2
Enter the number of columns: 3
Enter elements of the matrix:
1 2 3
4 5 6
Transpose of the matrix:
1 4
2 5
3 6

Solution 2: Using Functions for Modularity

2.1 Explanation

In this solution, we will use separate functions for input, finding the transpose, and printing the matrix. This modular approach enhances code readability and reusability.

2.2 Program

C
#include <stdio.h>

void inputMatrix(int matrix[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void transposeMatrix(int matrix[][10], int transpose[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
}

void printMatrix(int matrix[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int rows, cols;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    int matrix[10][10], transpose[10][10];

    printf("Enter elements of the matrix:\n");
    inputMatrix(matrix, rows, cols);

    transposeMatrix(matrix, transpose, rows, cols);

    printf("Transpose of the matrix:\n");
    printMatrix(transpose, cols, rows);

    return 0;
}

Output

C
Enter the number of rows: 2
Enter the number of columns: 3
Enter elements of the matrix:
1 2 3
4 5 6
Transpose of the matrix:
1 4
2 5
3 6

Solution 3: Using Pointers

3.1 Explanation

In this solution, we will use pointers to dynamically allocate memory for the matrices. This approach provides flexibility, especially for larger matrices or when the matrix size is determined at runtime.

3.2 Program

C
#include <stdio.h>
#include <stdlib.h>

void inputMatrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void transposeMatrix(int **matrix, int **transpose, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
}

void printMatrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int rows, cols;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    int **matrix = (int **)malloc(rows * sizeof(int *));
    int **transpose = (int **)malloc(cols * sizeof(int *));
    for (int i = 0; i < rows; i++) {
        matrix[i] = (int *)malloc(cols * sizeof(int));
    }
    for (int i = 0; i < cols; i++) {
        transpose[i] = (int *)malloc(rows * sizeof(int));
    }

    printf("Enter elements of the matrix:\n");
    inputMatrix(matrix, rows, cols);

    transposeMatrix(matrix, transpose, rows, cols);

    printf("Transpose of the matrix:\n");
    printMatrix(transpose, cols, rows);

    for (int i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    for (int i = 0; i < cols; i++) {
        free(transpose[i]);
    }
    free(matrix);
    free(transpose);

    return 0;
}

Output

C
Enter the number of rows: 2
Enter the number of columns: 3
Enter elements of the matrix:
1 2 3
4 5 6
Transpose of the matrix:
1 4
2 5
3 6

Conclusion

Using C Program to Find Transpose of a Matrix using multi-dimensional arrays can be implemented in several ways. The simple iterative approach is straightforward and easy to understand. Using functions for modularity enhances code readability and reusability. Utilizing pointers provides flexibility in handling dynamic memory allocation, making it suitable for larger matrices or when the matrix size is determined at runtime. Understanding these methods will help you efficiently handle matrix operations in C programming.