Original matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Transpose of Matrix
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
For Right Rotate -->( For right rotate we need to reverse all the row in transpose matrix )
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
For Left Rotate -->( For left rotate we need to reverse all the column in transpose matrix )
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
CODE --->
#include<stdio.h>
#define n 5
void print(int arr[][5]){
int i , j ;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
void transpose(int arr[][5]){
int i,j;
for(i=0;i<n;i++){
for(j=i;j<n;j++){
int temp = arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp ;
}
}
}
void rotateright(int arr[][5]){
int i,j,k;
for(i=0;i<n;i++){
j=0;
k=n-1;
while(j<k){
int temp=arr[i][j];
arr[i][j++]=arr[i][k];
arr[i][k--]=temp ;
}
}
printf("after the right rotation value is ---> \n \n ");
print(arr);
}
void main(){
int arr[][5]= {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
transpose(arr);
rotateright(arr);
}
No comments:
Post a Comment