fork download
  1. //
  2. // main.cpp
  3. // Array Rotation
  4. //
  5. // Created by Himanshu on 18/09/21.
  6. //
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11. const int N = 5;
  12.  
  13. void printArray (int A[]) {
  14. for (int i=0; i<N; i++) {
  15. cout<<A[i]<<" ";
  16. }
  17. cout<<endl;
  18. }
  19.  
  20. void RotateByOneElement(int A[]) {
  21. int temp = A[0];
  22.  
  23. for(int i=0; i<N-1; i++) {
  24. A[i] = A[i+1];
  25. }
  26. A[N-1] = temp;
  27. }
  28.  
  29. void Rotate (int A[], int d) {
  30. cout<<"Array:"<<endl;
  31. printArray(A);
  32. for (int i=1; i<=d; i++) {
  33. RotateByOneElement(A);
  34. cout<<"Array after "<<i<<" rotation:"<<endl;
  35. printArray(A);
  36. }
  37. }
  38.  
  39. int main() {
  40. int A[N] = {5, 2, 4, 6, 1};
  41. int d = 4;
  42. Rotate(A, d);
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
Array:
5 2 4 6 1 
Array after 1 rotation:
2 4 6 1 5 
Array after 2 rotation:
4 6 1 5 2 
Array after 3 rotation:
6 1 5 2 4 
Array after 4 rotation:
1 5 2 4 6