The problem statement here is, to write a C program to rotate a given square matrix by 90 degrees anticlockwise. To implement this, we need the size of the square matrix and the elements of the square matrix (2D array) as inputs. Our desired output is also a matrix or 2D array after completion of 90 degree rotation in the anticlockwise direction.
The first step is always to gather the necessary inputs. To do so, we can make use of predefined input function, scanf() in C language. This function reads any primitive datatype input at runtime from the keyboard. The type of input is determined by the format specifier used and since all our inputs are of integer type, we use ‘%d’ as format specifier for them all.
So first, we will read the size of square matrix (n) following which we create the array of the same size (a[n][n]) dynamically using calloc() function. Then, we iterate through the array and read all the elements of it.
printf(“\nEnter the N value for square matrix :”);
scanf(“%d”,&n);
a= (int **)calloc(n,sizeof(int*));
for (i=0; i<n; i++)
a[i] =(int *)calloc(n,sizeof(int*));
printf(“\nEnter the elements in %dX%d matrix :”,n,n);
for (i = 0; i<n;i++) {
for(j=0;j<n; j++) {
scanf(“%d”,&a[i][j]);
}
}
We then display the 2D array in matrix form with every row in a new line before rotation, for the better understanding of the user.
To rotate a matrix 90 degrees in anticlockwise direction, we need to first transpose it and then, reverse the each column. To transpose a matrix, we have to interchange the rows and columns i.e., swap a[i][j] and a[j][i]. Therefore, we iterate through the rows and columns and for every i<j we swap a[i][j] with a[j][i] using temporary variable (t). We do so only for i<j because for its corresponding j<i it would have already been swapped by then and swapping it twice will get us back our original matrix itself.
for (i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(i<j) {
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
}
After transposing the matrix, we have to reverse ever column in the matrix or 2D array. So we iterate through every column and swap the first (0) with the last(n-1) and, increment the first index (1) and decrement the last index (n-2) and swap them. This continues until we reach the mid value and by then, the entire column would be reversed as follows:
for (i = 0; i < n; i++) {
for(j = 0,k=n-1; j<k; j++,k–) {
t=a[j][i];
a[j][i]=a[k][i];
a[k][i]=t;
}
}
This newly updated matrix is nothing but, our desired resultant output i.e., the matrix after 90 degree rotation in anticlockwise direction. This matrix is then displayed on the console screen using predefined output function, printf() in matrix format with every row in a new line.
for (i = 0; i < n;i++) {
for (j = 0; j < n; j++) {
printf(“%d\t”, a[i][j]);
}
printf(“\n”);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <stdio.h> #include <stdlib.h> int main() { int n,i, j,k,**a ,t; printf("\nEnter the N value for square matrix :"); scanf("%d",&n); a= (int **)calloc(n,sizeof(int*)); for (i=0; i<n; i++) a[i] =(int *)calloc(n,sizeof(int*)); printf("\nEnter the elements in %dX%d matrix :",n,n); for (i = 0; i<n;i++) { for(j=0;j<n; j++) { scanf("%d",&a[i][j]); } } printf( "The matrix before rotation \n"); for (i = 0; i < n;i++) { for (j = 0; j < n; j++) { printf("%d\t", a[i][j]); } printf("\n"); } for (i = 0; i < n; i++) { for(j = 0; j < n; j++) { if(i<j) { t=a[i][j]; a[i][j]=a[j][i]; a[j][i]=t; } } } printf("\n"); for (i = 0; i < n; i++) { for(j = 0,k=n-1; j<k; j++,k--) { t=a[j][i]; a[j][i]=a[k][i]; a[k][i]=t; } } printf( "The matrix after rotation - AntiClockwise\n"); for (i = 0; i < n;i++) { for (j = 0; j < n; j++) { printf("%d\t", a[i][j]); } printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Enter the N value for square matrix :4 Enter the elements in 4X4 matrix : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The matrix before rotation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The matrix after rotation - AntiClockwise 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 |
Rotate a Given Matrix by 90 Degree Using Function
We have discussed the logic to rotate a given matrix in anticlockwise direction. We can notice that, the code is very lengthy and is placed within main method only. The disadvantages it has is that, if some part of code is to be found then, it can take a lot of time to do so.
Also, if the same logic is required elsewhere in the code, we’ll have to rewrite the entire thing again.
So, to make the code reusable and enhance the readability of the code, we can make use of functions. We can split the code based on its functionality and place it within separate function block and invoke it when required. This way, it becomes more clean and readable and also, we can reuse it my making function call and passing necessary parameters whenever required.
To implement the same, we first read the size of square matrix and create the 2D arrray of the same size dynamically using calloc(). Then, we call the input() function which reads the element of the array.
Following this, we display the array before rotation in matrix format by calling the print() function. Later, we transpose the input array using transpose() function and then, reverse the columns of the array using reverse_columns() function. This resultant array is our desired output which we display by invoking the print() function again.
Within each function, the logic and set of statements are the same as discussed and same is the time and space complexity. But the difference here is that, the readability of the code has improved a lot and each of this function block is now reusable as many times as required within the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#include <stdio.h> #include <stdlib.h> void reverse_columns(int **a,int n) { int i,j,k,t; for (i = 0; i < n; i++) { for(j = 0,k=n-1; j<k; j++,k--) { t=a[j][i]; a[j][i]=a[k][i]; a[k][i]=t; } } } void transpose(int **arr1,int x) { int i,j,t; for (i = 0; i < x; i++) { for(j = 0; j < x; j++) { if(i<j) { t=arr1[i][j]; arr1[i][j]=arr1[j][i]; arr1[j][i]=t; } } } } void print(int **arr1,int n) { int i,j; for (i = 0; i<n;i++) { for (j = 0; j<n; j++) { printf("%d\t", arr1[i][j]); } printf("\n"); } printf("\n"); } void input(int **arr1,int n) { int i,j; for (i = 0; i<n;i++) { for(j=0;j<n; j++) { scanf("%d",&arr1[i][j]); } } } int main() { int n,i, j,k,**a ,t; printf("\nEnter the N value for square matrix :"); scanf("%d",&n); a= (int **)calloc(n,sizeof(int*)); for (i=0; i<n; i++) a[i] =(int *)calloc(n,sizeof(int*)); printf("\nEnter the elements in %dX%d matrix :",n,n); input(a,n); printf( "The matrix before rotation \n"); print(a,n); transpose(a,n); reverse_columns(a,n); printf( "The matrix after rotation - AntiClockwise\n"); print(a,n); return 0; } |