An Algorithm for Shifting

closed account (zb0S216C)
I'm looking for an algorithm that is capable of shifting all objects within an array n places to the left and/or right. For instance:

1
2
int Array_A[] = {1, 2, 3, 4, 5, 0, 0};
int Array_B[] = {0, 0, 1, 2, 3, 4, 5};

Are there any possibilities? If not, how could I implement one? I've been trying to implement my own, but it seems the pointers always end up in the wrong place.

Thanks.

Wazzak
Last edited on
C++ has a function for that

in-place: std::rotate(Array_A, Array_A+5, Array_A+7);
copying to B: std::rotate_copy(Array_A, Array_A+5, Array_A+7, Array_B);
closed account (zb0S216C)
That'll do. Thanks, Cubbi :)

Wazzak
Topic archived. No new replies allowed.