-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRotate.c
57 lines (54 loc) · 1.17 KB
/
Rotate.c
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
#include<stdio.h>
#include<stdlib.h>
void rotate(int a[],int n,char d,int cr)
{
int *b,i,index;
b = (int*) calloc(sizeof(int),n);
for (i = 0; i < n; i++)
{
if(d=='r')
{
index = i+cr;
if(index > n-1)
{
index -= n;
}
*(b+index) = *(a+i);
}
else{
index = i-cr;
if(index < 0)
{
index += n;
}
*(b+index) = *(a+i);
}
}
for (i = 0; i < n; i++)
{
*(a+i) = *(b+i);
}
}
void main()
{
int a[100],n,rt;
char c;
printf("Enter no. of elements of array: ");
scanf("%d",&n);
printf("Enter elements of array: ");
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
getchar();
printf("Enter direction of rotation: ");
scanf("%c",&c);
printf("Length of rotation: ");
scanf("%d",&rt);
rotate(a,n,c,rt);
printf("Rotated array is : \n");
for(int i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}