Rotate an array by 2 places Ex:array a[]={0,1,2,3,4}; final : 3,4,0,1,2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ansark
    New Member
    • May 2010
    • 2

    Rotate an array by 2 places Ex:array a[]={0,1,2,3,4}; final : 3,4,0,1,2

    Can any one write the code by using c programming for this query.

    Thanks!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Yes, you can!

    Is the rotation always 2 places? Are you using c or c++?
    I suggest using 2 temporary variables to hold the last 2 variables, (3 and 4)
    Then rotate the rest of the matrix. Then put values 3 and 4 back in.

    Comment

    • ansark
      New Member
      • May 2010
      • 2

      #3
      Originally posted by jkmyoung
      Yes, you can!

      Is the rotation always 2 places? Are you using c or c++?
      I suggest using 2 temporary variables to hold the last 2 variables, (3 and 4)
      Then rotate the rest of the matrix. Then put values 3 and 4 back in.
      Thank you very much for your reply. I am using C. cant we do without matrix this program ?

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        There is a C++ generic algorithm 'rotate()' which performs this function found in the <algorithm> header.e.g.
        Code:
        char * s="ABCDEF";
        rotate(s,s+4,s+6);
        //prints EFABCD

        Comment

        Working...