-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patharrayRotation.cpp
More file actions
57 lines (46 loc) · 1 KB
/
arrayRotation.cpp
File metadata and controls
57 lines (46 loc) · 1 KB
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
/*
Question : Write a function that rotates array[] of
size 'size' by 'numberOfRotations' times.
*/
#include <iostream>
using namespace std;
int * rotateByOne(int array[]) {
int temp;
temp = array[0];
int i = 0;
while (array[i] != '\0') {
array[i] = array[i+1];
i++;
}
array[i-1] = temp;
return array;
}
void display(int array[]){
int i = 0;
while (array[i] != '\0') {
cout << array[i] << '\t';
i++;
}
}
int main(int argc, char const *argv[]) {
int array[20];
int *rotated;
int size;
int numberOfRotations;
cout << "Enter the size of array: ";
cin >> size;
cout << "Enter the elements of array: ";
for (int i = 0; i < size; ++i) {
cin >> array[i];
}
array[size] = '\0';
cout << "How many rotations you want: ";
cin >> numberOfRotations;
for (int i = 0; i < numberOfRotations; ++i) {
rotated = rotateByOne(array);
cout << "Array after rotation " << i+1 << endl;
display(rotated);
cout << '\n';
}
return 0;
}