Rotate the given array by a single step multiple times to shift the initial element of the array to the end. This action essentially moves the first element to the last position while all other elements shift towards the left.
For better understanding, we can approach this issue as follows. Initially, the item at index 0 is selected, and the item at the first index moves to the left, taking the index 0 position. Similarly, the item at the second index moves to the first index. This process continues with all items shifting to the left, leaving the last index empty. The empty index is then filled with the first element from the array that was initially removed. This entire process is repeated for a specified number of iterations.
Input format:
Array of integers int array
Number of rotations n
Output:
Print the array after n rotations
Example for illustration:
Array = {10, 20, 30, 40, 50, 60, 70, 80}
Num = 4;
Initial array: {10, 20, 30, 40, 50, 60, 70, 80}
After a single rotation: {20, 30, 40, 50, 60, 70, 80, 10}
After the second iteration: {30, 40, 50, 60, 70, 80, 10, 20}
After the third shift: {40, 50, 60, 70, 80, 10, 20, 30}
After the fourth iteration: {50, 60, 70, 80, 10, 20, 30, 40}
Example:
The resolution for the aforementioned issue is provided as follows:
#include<iostream>
using namespace std;
void reverseFunction(int arr[], int first, int last)
{
while (first < last) {
int temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
first++;
last--;
}
}
int main(){
int n;
cout << "Enter the length of the array " << endl;
cin >> n;
int nums[n];
cout << "Enter " << n << " numbers for the array" << endl;
for(int i=0; i<n; i++){
cin >> nums[i];
}
int rotations;
cout << "Enter the number of rotations to rotate " << endl;
cin >> rotations;
rotations = rotations % n;
reverseFunction(nums, 0, rotations-1);
reverseFunction(nums, rotations, n-1);
reverseFunction(nums, 0, n-1);
cout << "Array after the " << rotations << " rotations" << endl;
for(int i=0; i<n; i++){
cout << nums[i] << " ";
}
return 0;
}
Output:
Explanation:
This program consists of two functions: the main function and a function dedicated to reversing an array. Its purpose is to rotate the array. Initially, the program prompts the user to input the array's length, followed by entering the elements to be stored in the array. Subsequently, it requests the number of rotations to perform on the array. Next, the reverseFunction is invoked, which accepts three parameters: an integer array, the starting index, and the ending index. This reverseFunction effectively reverses the specified integer array from the starting index to the ending index. Initially, the function is called with the parameters array, zero, and one less than the number of rotations. Subsequently, another call is made to reverseFunction with the parameters array, the number of rotations, and one less than the array's size. Finally, the reverseFunction is invoked one last time with the parameters integer array, zero, and one less than the array's size. Following this process, the array undergoes rotation, and the resulting array is then displayed.
The operation of the reverseFunction involves taking an array and two specified indices as parameters. Through the implementation of a while loop, it systematically exchanges elements located at the initial and final indices, gradually progressing towards the middle of the array. This iterative swapping action effectively alters the order of elements within the array segment defined by the given indices. By continuing the swapping process until the first and last indices converge, the reverseFunction accomplishes the complete reversal of the designated array section. This technique, rooted in the concept of basic element interchange, illustrates a core methodology commonly employed in software development for activities such as array manipulation and reordering. The strength of this approach lies in its uncomplicated nature, making it both instinctive and beneficial for carrying out array reversal tasks.
Conclusion:
In summary, the C++ program effectively rotates an array by a designated number of positions through the implementation of a reversal algorithm. It offers a user-friendly interface, enabling smooth input and displaying the rotated array correctly. The code exhibits a comprehensive grasp of array manipulation methods, presenting a viable solution.