-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharray_rotation.c
More file actions
60 lines (55 loc) · 992 Bytes
/
array_rotation.c
File metadata and controls
60 lines (55 loc) · 992 Bytes
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
#include<stdio.h>
#define LIMIT 1000
void rotateLeftByOne(int *arr,int n)
{
int temp = arr[0];
for(int i=0;i<n-1;i++)
{
arr[i] = arr[i+1];
}
arr[n-1] = temp;
}
void rotateRightByOne(int *arr,int n)
{
int temp = arr[n-1];
for(int i=n-1;i>=0;i--)
{
arr[i] = arr[i-1];
}
arr[0] = temp;
}
void rotate(int *arr,int steps,int n,char l)
{
if(l == 'l')
for(int i=0;i<steps;i++)
{
rotateLeftByOne(arr,n);
}
else if(l == 'r')
for(int i=0;i<steps;i++)
{
rotateRightByOne(arr,n);5
}
}
void display(int *arr,int n)
{
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
int main(void)
{
char label;
int n,arr[LIMIT],steps;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&steps);
scanf(" %c",&label);
rotate(arr,steps,n,label);
display(arr,n);
return 0;
}