-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_array_left.c
More file actions
55 lines (39 loc) · 874 Bytes
/
rotate_array_left.c
File metadata and controls
55 lines (39 loc) · 874 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
#include <stdio.h>
void rotate_array(int *, int, int);
void print_array(int *, int);
void main()
{
printf("** Rotate array left demo **\n");
int nums[] = { 1, 2, 3, 4, 5 };
int k;
printf("Enter position to shift\n");
scanf("%d", &k);
if (k > sizeof(nums) / sizeof(nums[0])) {
printf("Invalid size\n");
return;
}
rotate_array(nums, 5, k);
}
void rotate_array(int *nums, int numsSize, int k)
{
int i = 0;
printf("Original array is : \n");
print_array(nums, numsSize);
if (numsSize == 1 || k == 0) return;
int temp;
for (int j = 0; j < k; j++ ) {
temp = nums[0];
for (i = 0 ; i < numsSize -1; i++) {
nums[i] = nums[i+1];
}
nums[numsSize -1] = temp;
}
printf("Final array is : \n");
print_array(nums, numsSize);
}
void print_array(int *nums, int size)
{
for (int i = 0; i < size; i++)
printf("%2d ", nums[i]);
printf("\n");
}