Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to Rotate a matrix by 90 degrees in the clockwise direction in C
In this article, we are given an N x N matrix, and our task is to rotate it by 90 degrees clockwise in C.
Syntax
void rotateMatrix(int matrix[N][N]); // For brute force approach void rotateMatrixInPlace(int matrix[N][N]); // For in-place rotation approach
Example
Input: [1 2 3], [4 5 6], [7 8 9] Output: [7 4 1], [8 5 2], [9 6 3]
Below are different approaches to rotate a matrix by 90 degrees in the clockwise direction
Method 1: Using Brute Force Approach
This is the simple and direct approach to understand and implement. In this approach, we create a new matrix and manually place each element at its correct rotated position.
- Define a function and take the given matrix and its size as input parameters.
- Create a new matrix of the same size. Iterate through the elements and place them in their new correct rotated positions.
- Copy the rotated matrix back to the original matrix.
Example
Here is an example code implementing above steps to rotate a matrix by 90 degrees in the clockwise direction using brute force approach ?
#include <stdio.h>
#define N 3
void rotateMatrix(int matrix[N][N]) {
int rotated[N][N];
/* Copy elements to rotated positions */
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
rotated[j][N - 1 - i] = matrix[i][j];
}
}
/* Copy back to original 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("
");
}
}
int main() {
int matrix[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Original Matrix:
");
printMatrix(matrix);
rotateMatrix(matrix);
printf("Rotated Matrix:
");
printMatrix(matrix);
return 0;
}
Original Matrix: 1 2 3 4 5 6 7 8 9 Rotated Matrix: 7 4 1 8 5 2 9 6 3
Method 2: Using In-Place Rotation
This is a space-optimized approach. We make modifications in the input matrix without taking a new matrix. In this approach, we will rotate the matrix in place by first transposing the matrix and then reversing each row.
- Define a function and take the matrix and its size as input.
- Transpose the matrix (converting rows into columns and vice-versa).
- Reverse each row to obtain the required rotated matrix.
Example
The following example code uses in-place rotation to rotate a matrix by 90 degrees in the clockwise direction ?
#include <stdio.h>
#define N 3
void rotateMatrixInPlace(int matrix[N][N]) {
/* Step 1: Transpose the matrix */
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
/* Step 2: Reverse each row */
for (int i = 0; i < N; i++) {
for (int j = 0; j < N / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][N - 1 - j];
matrix[i][N - 1 - j] = temp;
}
}
}
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("
");
}
}
int main() {
int matrix[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Original Matrix:
");
printMatrix(matrix);
rotateMatrixInPlace(matrix);
printf("Rotated Matrix:
");
printMatrix(matrix);
return 0;
}
Original Matrix: 1 2 3 4 5 6 7 8 9 Rotated Matrix: 7 4 1 8 5 2 9 6 3
Comparison
Here is a comparison of time and space complexity of both approaches ?
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute Force | O(n²) | O(n²) |
| In-Place Rotation | O(n²) | O(1) |
Conclusion
Matrix rotation in C can be achieved using either brute force (simple but uses extra space) or in-place rotation (space-efficient using transpose and row reversal). The in-place approach is preferred for memory-constrained environments.
