C Program To Circular Rotation of an Array by n positions
Circular Array Rotation means rotating the elements in the array where one rotation operation moves the last element of the array to the first position and shifts all remaining elements to the right.
Here, we are given an unsorted array and our task is to perform the circular rotation by n number of rotations where n is a natural number. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.
Initial Array: [ 1 2 3 4 5 ]
After one rotation : [ 5 1 2 3 4 ]
After two rotation : [ 4 5 1 2 3 ]
Program 1: Circular Rotation of an Array by n Position
In this method, we directly perform the circular rotation using a temporary variable. Using two for loops, this rotation can be done. The first for loop was for the number of rotations and the second for loop shifts the array by one.
After the first for loop, a temporary variable is declared which holds the last element of the array. Later after the execution of the second for loop, the first element of the array holds this temp variable. And the resulting array is displayed.
Algorithm
- Start
- Declare an array
- Initialize the array
- Enter the index for circular rotation.
- Perform circular operation.
- Use two for loops and a temporary variable for the same.
- Store the last element of the array in the temporary variable.
- Using the second for loop shift element of the array by one.
- The last element of the array will be added to the start of the array.
- The resulting array is displayed at the end.
- Stop.
-
Below is the code for Circular Rotation of an Array by using the C language.
#include <stdio.h>
int main()
{
//Declare the length of the array
int length;
printf("Enter the length of the array ");
scanf("%d",&length);
//Declare an array
int arr[length];
printf("Enter the array elements ");
for(int i=0;i<length;i++) //Initialize array
scanf("%d",&arr[i]);
//n Enter the index for circular rotation i.e., the number of times the array should rotate
int n;
printf("Enter the index of rotation ");
scanf("%d",&n);
//Displays original array
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
//Perform circular rotation for n times
for(int i = 0; i < n; i++)
{
int j, temp;
//Stores the last element of the array
temp = arr[length-1];
for(j = length-1; j > 0; j--)
{
//Shift element of array by one
arr[j] = arr[j-1];
}
//Last element of the array will be added to the start of the array.
arr[0] = temp;
}
printf("\n");
//Display the array after rotation
printf("Array after circular rotation: \n");
for(int i = 0; i< length; i++){
printf("%d ", arr[i]);
}
return 0;
}
Enter the length of the array 5
Enter the array elements 2 3 4 5 6
Enter the index of rotation Original array: 2
2 3 4 5 6
Array after circular rotation:
5 6 2 3 4
Program 2: Circular Rotation of an Array by n Position
In this method, three functions are called. The first function is for circular rotation. This function then calls another function that is responsible for reversing the array. At the end, another function is called which is responsible for printing the array.
Algorithm
- Start
- Declare an array
- Initialize the array
- Enter the index for circular rotation.
- Call a function that will perform the circular operation.
- This function will call another function that will reverse the array.
- This function will declare a temporary variable.
- Using this temporary variable, elements are swapped as per the rotation index.
- Now print the resulting array.
- Stop.
Below is the code for Circular Rotation of an Array by using the C language.
#include <stdio.h>
void reverseArray(int arr[], int start, int end) //Function to rotate the array
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
void circularRotate(int arr[], int d, int n) //Function for circular Rotation
{
reverseArray(arr, 0, n - 1);
reverseArray(arr, 0, d - 1);
reverseArray(arr, d, n - 1);
}
void printArray(int arr[], int size) //Function to print Array
{
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
}
/* Driver program to test above functions */
int main()
{
int n; //Declare Array size
printf("\nEnter the number of elements ");
scanf("%d",&n);
int arr[n]; //Declare an array
printf("\nEnter the array elements ");
for(int i=0;i<n;i++) //Initialize array elements
scanf("%d",&arr[i]);
int k; //Declare a variable for number of array rotation
printf("\nEnter the number of array rotation ");
scanf("%d",&k);
circularRotate(arr, k, n); //Call a function for circular rotation
printf("\nAfter %d array rotation, the resulting array is \n",k);
printArray(arr, n); //Call a function to print the array
return 0;
}
Enter the number of elements 5
Enter the array elements 7 6 5 9 8
Enter the number of array rotation 2
After 2 array rotation, the resulting array is
9 8 7 6 5