Transpose a 2D Array in C

To transpose a 2D array (matrix) in C, we swap the rows and columns of the matrix. This means that the element at position [i][j] in the original matrix is placed at position [j][i] in the transposed matrix.


Examples of Transposing a Matrix in C

1. Transpose of a Square Matrix

In this example, we will take a square matrix (same number of rows and columns) and compute its transpose using nested loops. The transposed matrix will be stored in a separate matrix.

main.c

</>
Copy
#include <stdio.h>

#define SIZE 3

int main() {
    int matrix[SIZE][SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int transpose[SIZE][SIZE];

    // Compute the transpose
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print the transposed matrix
    printf("Transposed Matrix:\n");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We define a constant SIZE to specify a 3x3 matrix.
  2. matrix[][] stores the original matrix, and transpose[][] stores the transposed matrix.
  3. We use two nested loops:
    • The outer loop iterates over rows.
    • The inner loop iterates over columns, swapping matrix[i][j] to transpose[j][i].
  4. Finally, we print the transposed matrix using another nested loop.

Output:

Transposed Matrix:
1 4 7
2 5 8
3 6 9

2. Transpose of a Non-Square Matrix

In this example, we will transpose a non-square matrix (rows ≠ columns) by swapping the elements and storing them in a new matrix.

main.c

</>
Copy
#include <stdio.h>

#define ROWS 2
#define COLS 3

int main() {
    int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}};
    int transpose[COLS][ROWS];

    // Compute the transpose
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print 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");
    }

    return 0;
}

Explanation:

  1. We define constants ROWS and COLS for a 2x3 matrix.
  2. matrix[][] stores the original matrix, and transpose[][] is declared with swapped dimensions (3x2).
  3. We use nested loops to swap matrix[i][j] into transpose[j][i].
  4. The transposed matrix is printed row by row.

Output:

Transposed Matrix:
1 4
2 5
3 6

3. Transposing a Matrix In-Place (Only for Square Matrices)

For square matrices, we can transpose the matrix in-place without using an additional array. We only swap the upper triangle with the lower triangle of the matrix.

main.c

</>
Copy
#include <stdio.h>

#define SIZE 3

int main() {
    int matrix[SIZE][SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    // Compute in-place transpose
    for (int i = 0; i < SIZE; i++) {
        for (int j = i + 1; j < SIZE; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    // Print the transposed matrix
    printf("Transposed Matrix:\n");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We use a single matrix[][] without a separate transpose matrix.
  2. Instead of swapping all elements, we swap only elements above the diagonal with their lower counterparts.
  3. A temporary variable temp is used to facilitate swapping.

Output:

Transposed Matrix:
1 4 7
2 5 8
3 6 9

Conclusion

We explored multiple methods to transpose a 2D matrix in C:

  1. Using an additional matrix (suitable for all matrices).
  2. In-place transposition (only for square matrices).