Advertisement
Divyansh_Chourey

matrix rotation

Apr 5th, 2024
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. int main(){
  3.  
  4.     int mat1[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  5.     int mat2[3][2];
  6.  
  7.     printf("Original matrix:\n");
  8.  
  9.     for(int i = 0; i<3; i++){
  10.         for(int j=0; j<3; j++){
  11.             printf("%d ", mat1[i][j]);
  12.         }
  13.         printf("\n");
  14.     }
  15.  
  16.     printf("Rotated matrix:\n");
  17.  
  18.     mat2[0][0] = mat1[2][0];
  19.     mat2[0][1] = mat1[1][0];
  20.     mat2[0][2] = mat1[0][0];
  21.  
  22.     mat2[1][0] = mat1[2][1];
  23.     mat2[1][1] = mat1[1][1];
  24.     mat2[1][2] = mat1[0][1];
  25.  
  26.     mat2[2][0] = mat1[2][2];
  27.     mat2[2][1] = mat1[1][2];
  28.     mat2[2][2] = mat1[0][2];
  29.  
  30.     for(int a = 0; a<3; a++){
  31.         for(int b=0; b<3; b++){
  32.             printf("%d ", mat2[a][b]);
  33.         }
  34.         printf("\n");
  35.     }
  36.    
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement