rotate function in C language
I am trying to write a rotate function in C language.
![```c
/**
* Function: rotate_right
* Description: "rotates" square matrix m 90 degrees right
* (clockwise).
*
* Example: consider the 3x3 matrix below:
*
* abc
* def
* hij
*
* After one rotation, the matrix contents are:
*
* hda
* ieb
* jfc
*
* Estimated Difficulty: LEVEL-3
*/
void rotate_right(char **m, int n);
```
**Explanation:**
This C function, `rotate_right`, is designed to rotate a square matrix 90 degrees clockwise. The example provided with the function illustrates how a 3x3 matrix is transformed through this rotation:
- The initial matrix:
```
abc
def
hij
```
- After a 90-degree clockwise rotation, the resulting matrix is:
```
hda
ieb
jfc
```
This demonstrates a fundamental operation in matrix manipulation, useful in various computational applications such as image processing and computer graphics. The function’s estimated difficulty level is 3, indicating a moderate level of complexity.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2Fca5f7c4f-4bbe-4616-904c-1758dc5da823%2F3fov9y_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Program:
#include <stdio.h>
#include <stdlib.h>
// declaration of row and columns in square matrix
const int M = 3;
const int N = 3;
// function to rotate square matrix clock-wise
void rotate_right(char a[M][N])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=2;j>=0;j--)
{
// print the result
printf("%c ",a[j][i]);
}
printf("\n");
}
}
// main function
int main()
{
// square matrix
char a[3][3] = {'a', 'b', 'c','d', 'e', 'f', 'h', 'i', 'j'};
int i,j;
// print the matrix
printf("Original Array\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%c ",a[i][j]);
}
printf("\n");
}
// function call of rotate_right function and print the result
printf("\nRotate Matrix by 90 degrees\n");
rotate_right(a);
return 0;
}
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)