Coders Packet

Rotation Of Array In C++

By Aditi Mishra

  • array_rotate.exe
  • a.exe
  • array_rotate.cpp
  • This project performs the Array rotation in C++ language, totally dependent on the user input to rotate the array, and finally, the resultant rotated array is produced.

    Rotation of array means we are shifting elements to the right side(clockwise) or left side. 

     

    My program is designed in C++ language in such a way that it performs the clockwise rotation that is right-side rotation pattern.


    This array rotation can be used in other ways also except just doing simply the rotation.

    In this program, array A is taken with a size N and number of rotations D. Here, According to the number of rotations given by the user, the array is executed.

     

    Suppose, if the size(N) of the array is 5 and inputs in an array (A) is 1 2 3 4 5 then, if (D) is rotation we take as 2, so the output is 3 4 5 1 2.

    This is how my program works. It depends on how many times the user wants to rotate that particular array. It's a benefit that in my program, it is not restricted that a number of rotations are fixed, it is variable depending on the user input value.

     

     

    CODE-->

     

    #include<bits/stdc++.h>
    using namespace std;
    
    void rotatearray_d(int A[],int N,int D)
    {
    while(D--) //loop running for number of rotations
    {
    int k=A[0];
    for(int i=0;i<N;i++)
    {
    A[i]=A[i+1];
    }
    A[N-1]=k;
    }
    
    cout<<"\n";
    }
    
    int main()
    {
    int T;
    cout<<"\nEnter The Number Of Testcases:-> ";
    cin>>T;
    
    while(T--) //loop running for no of testcases
    {
    int N,D;
    cout<<"\nEnter The Size Of Array:-> ";
    cin>>N;
    
    cout<<"\nEnter The Number Of Rotations:-> ";
    cin>>D;
    
    int A[N+1];
    
    cout<<"\nEnter the elements in array:-> ";
    for(int i=0;i<N;i++)
    {
    cin>>A[i];
    }
    cout<<"\n";
    
    cout<<"******************************************\n";
    cout<<"Array before rotation is:-> ";
    for(int i=0;i<N;i++)
    {
    cout<<A[i]<<" ";
    }
    cout<<"\n";
    cout<<"******************************************\n";
    
    rotatearray_d(A,N,D);
    
    cout<<"******************************************\n";
    cout<<"Array after rotation is:-> ";
    for(int i=0;i<N;i++)
    {
    cout<<A[i]<<" ";
    }
    cout<<"\n";
    cout<<"******************************************\n";
    
    }
    return 0;
    }

     

    Input- Enter The Number Of Testcases:-> 2

     

              Enter The Size Of Array:-> 5

     

              Enter The Number Of Rotations:-> 2

     

              Enter the elements in array:-> 1 2 3 4 5

     

    Output- ******************************************

                Array before rotation is:-> 1 2 3 4 5

                ******************************************

     

                ******************************************

                Array after rotation is:-> 3 4 5 1 2

                ******************************************

     

     

    Input- Enter The Size Of Array:-> 6

     

               Enter The Number Of Rotations:-> 3

     

               Enter the elements in array:-> 4 5 6 7 8 9

     

    Output- ******************************************

                 Array before rotation is:-> 4 5 6 7 8 9

                ******************************************

     

                ******************************************

                Array after rotation is:-> 7 8 9 4 5 6

                ******************************************

    Download Complete Code

    Comments

    No comments yet