-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotateM.cpp
77 lines (73 loc) · 2.03 KB
/
rotateM.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//rotate a matrix clockwise by 1
#include<iostream>
using namespace std;
int i,j;
void rotateM(int (*a)[10],int n, int m)
{
int x=0,y=0,p,c;
if(x+1==n||y+1==m)
break;
p =*(*(a+x+1)+y);
for(i=y;i<m;i++)
{
c=*(*(a+x)+i);
*(*(a+x)+i)=p;
p=c;
}
x++;
for(i=x;i<n;i++)
{
c=*(*(a+i)+m-1);
*(*(a+i)+n-1)=p;
p=c;
}
m--;
if(x<n)
{
for(i=m-1;i>=y;i--)
{
c=*(*(a+n-1)+i);
*(*(a+n-1)+i)=p;
p=c;
}
}
n--;
if(y<m)
{
for(i=n-1;i>=x;i--)
{
c=*(*(a+i)+y);
*(*(a+i)+y)=p;
p=c;
}
}
y++;
if(x<n && y<m)
{
rotateM(&a[0],n,m);
}
}
main()
{
int a[10][10],n,m;
cout<<"Enter no. of Rows and Columns:\n";
cin>>n>>m;
cout<<"\nEnter the Matrix:\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
rotateM(&a[0],n,m);
cout<<"\nResultant Matrix:\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}