Latest :

C Program : Rotate a Given Matrix by 90 Degrees Anticlockwise

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”);

}

Output:

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.

x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...