Array Rotations
- Program for array rotation
An array is an aggregate data structure to store a group of objects of the same or different types.Array can hold primitive as well as references. The array is the most efficient data structure for sorting and accessing a sequence of objects.
In this lesson we will rotate an array arr[] of n elements by d. d is the number of rotations.To rotate array by one we will store arr[0] in a temporary variable temp,move arr[1] to arr[0],arr[2] to arr[1] …and finally temp to arr[n-1].
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 3
Rotate arr[] by one 3 times
We get [2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7, 1, 2] after second rotation and finally [4, 5, 6, 7, 1, 2, 3,] after third rotation

Here is the source code……..please first try yourself.
#include
using namespace std;
int main()
{
int arr[10]={0},n,i,j,t;
while(1)
{
cout < arr[i];
cout n;
for(i=0;i<n;i++)
{
t=arr[0];
for(j=0;j<6;j++)
{
arr[j]=arr[j+1];
}
arr[j]=t;
}
for (i=0;i<7;i++)
cout << arr[i] << " ";
cout << endl;
}
return 0;
}
Leave a comment