forked from shivprime94/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_Rotation.cpp
64 lines (57 loc) · 1.37 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
61
62
63
64
#include <iostream>
using namespace std;
void once_rotation(float arr[], int no_elements)
{
int flag = arr[0];
for (int i = 0; i < no_elements - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[no_elements - 1] = flag;
}
float rotate_array(float arr[], int no_elements, int no_rotations)
{
float flag = 0;
if (no_elements == 0)
{
cout << "Empty Array" << endl;
}
else if (no_elements > 100)
{
cout << "Out of Bound" << endl;
}
else
{
for (int j = 0; j < no_rotations; j++)
{
once_rotation(arr, no_elements);
}
cout << "Final array after rotation is : ";
for (int j = 0; j <= no_elements - 1; j++)
{
cout << arr[j] << " ";
}
cout << endl;
}
return 0;
}
int main()
{
float arr[100];
int no_rotations, no_elements;
cout << "Enter the number of elements in an array: ";
cin >> no_elements;
if (no_elements > 0 && no_elements <= 100)
{
cout << "Enter the value of elements : ";
for (int i = 0; i < no_elements; i++)
{
cin >> arr[i];
}
cout << "Enter the number of rotations : ";
cin >> no_rotations;
}
rotate_array(arr, no_elements, no_rotations);
cout << endl;
return 0;
}