rotate function in C language

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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.
Transcribed Image Text:```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.
Expert Solution
Step 1

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;
}

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education