Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

10,484 questions

14,262 answers

573 users

How to rotate square matrix 90 degrees to the right in C

1 Answer

0 votes
#include <stdio.h>
 
#define LEN 3 
 
void print_matrix(int matrix[][LEN]) { 
    for (int i = 0; i < LEN; i++) { 
        for (int j = 0; j < LEN; j++) 
            printf("%2d ", matrix[i][j]); 
        printf("\n"); 
    } 
    printf("\n"); 
} 
 
void rotate_matrix_90_degrees_right(int matrix[][LEN]) { 
    for (int i = 0; i < LEN / 2; i++) { 
        for (int j = i; j < LEN - i - 1; j++) { 
            int tmp = matrix[i][j]; 
            matrix[i][j] = matrix[LEN - 1 - j][i]; 
            matrix[LEN - 1 - j][i] = matrix[LEN - 1 - i][LEN - 1 - j]; 
            matrix[LEN - 1 - i][LEN - 1 - j] = matrix[j][LEN - 1 - i]; 
            matrix[j][LEN - 1 - i] = tmp; 
        } 
    } 
} 
   
int main() 
{ 
    int matrix[LEN][LEN] = 
    { 
        {1, 2, 3}, 
        {4, 5, 6}, 
        {7, 8, 9} 
    }; 
   
    rotate_matrix_90_degrees_right(matrix); 
 
    print_matrix(matrix); 
   
    return 0; 
} 
 
 
 
/*
run:
 
 7  4  1 
 8  5  2 
 9  6  3 
  
*/

 




answered May 19 by avibootz
...