Rotate a 2D Array in C

To rotate a 2D array (matrix) in C, we need to transform the elements by rearranging their positions based on a rotation algorithm. Typically, a matrix can be rotated 90 degrees clockwise, 90 degrees counterclockwise, or 180 degrees.

In this tutorial, we will cover different approaches to rotate a matrix using loops and helper functions.


Examples to Rotate a 2D Array

1. Rotate a 2D Array 90 Degrees Clockwise

In this example, we will rotate a square matrix 90 degrees clockwise. The transformation works by moving elements from row to column while reversing the order.

main.c

</>
Copy
#include <stdio.h>
#define N 3

void rotate90Clockwise(int matrix[N][N]) {
    int rotated[N][N];

    // Rotate elements
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            rotated[j][N - 1 - i] = matrix[i][j];
        }
    }

    // Copy back rotated matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            matrix[i][j] = rotated[i][j];
        }
    }
}

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

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

    printf("Original Matrix:\n");
    printMatrix(matrix);

    rotate90Clockwise(matrix);

    printf("\nRotated Matrix (90 Degrees Clockwise):\n");
    printMatrix(matrix);

    return 0;
}

Explanation:

  1. We define a function rotate90Clockwise() to handle rotation.
  2. We create a new array rotated[][] to store the transformed elements.
  3. Each element from matrix[i][j] is assigned to rotated[j][N-1-i], shifting row elements to columns.
  4. We copy the rotated matrix back into the original matrix.
  5. The printMatrix() function displays the original and rotated matrices.

Output:

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

Rotated Matrix (90 Degrees Clockwise):
7 4 1
8 5 2
9 6 3

2. Rotate a 2D Array 90 Degrees Counterclockwise

In this example, we will rotate a square matrix 90 degrees counterclockwise. The transformation involves moving elements from columns back to rows in a reversed order.

main.c

</>
Copy
#include <stdio.h>
#define N 3

void rotate90Counterclockwise(int matrix[N][N]) {
    int rotated[N][N];

    // Rotate elements
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            rotated[N - 1 - j][i] = matrix[i][j];
        }
    }

    // Copy back rotated matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            matrix[i][j] = rotated[i][j];
        }
    }
}

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

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

    printf("Original Matrix:\n");
    printMatrix(matrix);

    rotate90Counterclockwise(matrix);

    printf("\nRotated Matrix (90 Degrees Counterclockwise):\n");
    printMatrix(matrix);

    return 0;
}

Explanation:

  1. We define a function rotate90Counterclockwise() to perform rotation.
  2. Each element matrix[i][j] is mapped to rotated[N-1-j][i], reversing the column order.
  3. We copy the rotated array back into the original matrix.
  4. The printMatrix() function displays both matrices.

Output:

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

Rotated Matrix (90 Degrees Counterclockwise):
3 6 9
2 5 8
1 4 7

Conclusion

In this tutorial, we explored different ways to rotate a 2D array (matrix) in C:

  1. 90 Degrees Clockwise: Moving elements from row to column in reversed order.
  2. 90 Degrees Counterclockwise: Moving elements from column to row in reversed order.

If you need to rotate the matrix 180 degrees clockwise, you may call the rotate90Clockwise() function twice. Similarly, to rotate the matrix 270 degrees clockwise, you may call the rotate90Clockwise() function thrice, or call the rotate90Counterclockwise() once.