You are on page 1of 3

Matrix Rotation(imp)

Description:
*need to rotate matrix either 90,180,270,360
*int n,int arr1[ ] [ ]
*to rotate arr1 in clk wise direction in S degrees(s is angle of rotation)
*for this matrix 3 types of operations

1.rotate arr1 in angle s input:A S


2.query the element row k and column L input:Q K L
3.update row as X & col as Y iput:X Y Z

Input:
2 [1,2][3,4], "U116"

1 2
3 4 ===> 1 2
3 6

2 [1,2],[3,4] "A90"

1 2
3 4 ===> 3 1
4 2
2 [1,2],[3,4] "Q11"

1 2
3 4===> 4

Code:

int n,int arr1[ ][ ],char [ ]opt

int p,q,x,y;
int arr2[ ][ ]=new int[2][2];
int arr3[ ][ ]=new int[2][2];

String str=String.valueOf(opt);
int i,j;
int size=n;

for(p=0;p<2;p++)
{
for(q=0;q<2;q++)
{
arr3[p][q]=arr[p][q];
}
}
if(opt[0]=='A')
{
if(str.equals("A90")
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
arr2[j][n-i-1]=arr1[i][j];
}
}
for(p=0;p<2;p++)
{
for(q=0;q<2;q++)
{
System.out.print(" "+arr2[p][q]+" ");
}
system.out.print("\n");
}
}
}
else if(str.equals("A180")
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
arr2[j][n-i-1]=arr1[i][j];
}
}
for(p=0;p<2;p++)
{
for(q=0;q<2;q++)
{
System.out.print(" "+arr2[p][q]+" ");
}
system.out.print("\n");
}
}
}
if(str.equals("A270")
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
arr2[j][n-i-1]=arr1[i][j];
}
}
for(p=0;p<2;p++)
{
for(q=0;q<2;q++)
{
System.out.print(" "+arr2[p][q]+" ");
}
system.out.print("\n");
}
}
}
else
{
for(p=0;p<2;p++)
{
for(q=0;q<2;q++)
{
System.out.print(" "+arr2[p][q]+" ");
}
System.out.print("\n");
}
}
else if(opt[0]=='Q')
{
if(opt[1]<'2' && opt[2]<'2')
{
int val1=opt[1]-'0';
int val2=opt[2]-'0';
System.out.print(arr1[val1][val2]);
}
}
else if(opt[0]=='U')
{
if(opt[1]<'2' && opt[2]<'2')
{
arr1[opt[1]-'0'
System.out.print(arr1[val1][val2]);
}
}

You might also like