is it possible to rotate an array more than one position?

Here is the part of code I wrote but this moves the array only one position to the right...-----------------------------------------------------------------
cout<<"How many positions to the right do you want the array to be shifted? \n";
cin>>shift;
temp = array[size-1];
for(int i=size-1;i>0;i--)
{
array[i]=array[i-1];
}
array[0] = temp;


-=---------------------------------------------------------
Thisp rogram works perfectly but only if i want to shift one position.
I know I am close and I'm trying hard for the last couple of days to get this done ut just doesn't seem to work :(
Can any one help me here?
Last edited on
array[i]=array[i-DISTANCE_TO_SHIFT];

Don't forget to make sure you don't try to read/write off the ends of the array and all the other junk.

Alternatively, do what you're already doing more than once.

1
2
3
4
5
6
7
8
9
for (int b=0; b<number_of_times_to_shift; ++b)
{
  temp = array[size-1];
  for(int i=size-1;i>0;i--)
  {
    array[i]=array[i-1];
  }
  array[0] = temp;
}
Last edited on
Moschops, works perfectly! Thanks so much. I believe I was missing one additional for loop as you correctly pointed out.
Topic archived. No new replies allowed.