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 left 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_left(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[j][LEN - 1 - i]; 
            matrix[j][LEN - 1 - i] = matrix[LEN - 1 - i][LEN - 1 - j]; 
            matrix[LEN - 1 - i][LEN - 1 - j] = matrix[LEN - 1 - j][i]; 
            matrix[LEN - 1 - j][i] = tmp; 
        } 
    } 
} 
  
int main() 
{ 
    int matrix[LEN][LEN] = 
    { 
        {1, 2, 3}, 
        {4, 5, 6}, 
        {7, 8, 9} 
    }; 
  
    rotate_matrix_90_degrees_left(matrix); 

    print_matrix(matrix); 
  
    return 0; 
} 



/*
run:

 3  6  9 
 2  5  8 
 1  4  7 
 
*/

 




answered May 18 by avibootz
edited May 19 by avibootz
...