-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patharray_rotation.cpp
60 lines (47 loc) · 1.09 KB
/
array_rotation.cpp
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 <iostream>
using namespace std;
// Function to print array elements
void display(int arr[],int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
//Function to left rotate array
void leftrotate(int arr[], int size)
{
int i = 0;
int temp = arr[i];
for (i=0; i<size;i++)
arr[i]=arr[i+1];
arr[size-1] = temp;
}
int main()
{
int size, i, rotation;
cout << "Enter the array size : ";
cin >> size;
int arr[size] = {0};
cout << "Enter array values : ";
for (int i = 0; i < size; i++)
cin >> arr[i];
cout << "Array Elements : ";
display(arr,size);
cout << "\nEnter number of rotation : ";
cin>>rotation;
for (i=0; i<rotation;i++)
leftrotate(arr,size); // Function calling
cout << "\nRotated Array : ";
display(arr,size);
return 0;
}
/*
Input: Enter the array size : 5
Enter array values : 10
20
30
40
50
Array Elements : 10 20 30 40 50
Enter number of rotation : 2
Output: Rotated Array : 30 40 50 10 20
*/