-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_arr_to_left.c
More file actions
58 lines (49 loc) · 996 Bytes
/
rotate_arr_to_left.c
File metadata and controls
58 lines (49 loc) · 996 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
/*
* Author: Robert Dragan
* Github: nucata
* Date: 04-10-2022
* Description: Rotate an array one or n indexes to the left.
*/
#include <stdio.h>
void rotate_one_left(int *arr, size_t size) {
int tmp = arr[0];
int len = (int) size;
int i = 0;
while (i < len - 1) {
arr[i] = arr[i + 1];
i++;
}
arr[i] = tmp;
}
void rotate_n_left(int *arr, size_t size, int n) {
while (n > 0) {
rotate_one_left(arr, size);
n--;
}
}
int test(int *out, int *test, size_t size) {
int len = (int) size;
int i = 0;
while (i < len) {
if (!(out[i] == test[i])) {
printf("# Test Failed!\n");
return (0);
}
i++;
}
printf("# Test Succeed!\n");
return (0);
}
int main(void) {
int a1[] = {3, 9, 8, 1, 7, 6};
int a2[] = {1, 2, 3, 4, 5};
int t1[] = {9, 8, 1, 7, 6, 3};
int t2[] = {3, 4, 5, 1, 2};
size_t s1 = sizeof(a1) / sizeof(int);
size_t s2 = sizeof(a2) / sizeof(int);
rotate_one_left(a1, s1);
rotate_n_left(a2, s2, 2);
test(a1, t1, s1);
test(a2, t2, s2);
return (0);
}