# Thread: shifting in a circle

1. ## shifting in a circle

Hi,
in this code I'm trying to make a function that will perform a circulated shift of the numbers in an array, for example:
123 become 312
I have a bug in the code that I don't find !
(the user give the times for shifting)
thank for the helpers

Code:
```#include<stdio.h>

void shift_to_end(int array[],int size)
{
int i = 0, temp = 0;
for(i = size; i > 0 ; --i)
{
temp = array[i];
array[i] = array[i-1];
array[0] = temp;
}

}

#define ARRAY_SIZE 10

int main()
{
int numbers[ARRAY_SIZE];
int i = 0, shift = 0 ;

for (i = 0; i < ARRAY_SIZE ; ++i)
scanf(" %d", &numbers[i]);

scanf("%d", &shift);

for (i=1; i <= shift; ++i);
shift_to_end(numbers, ARRAY_SIZE);

/* print the shifted array */
for (i = 0; i < ARRAY_SIZE; ++i)
printf("%d", numbers[i]);

return 0;

}```

2. From a quick glance,

for(i = size; i > 0 ; --i)
...
temp = array[i];
Array indices start at 0, and end at size-1.

3. In the first time the for-loop is executed, you just loose array[0].
The best thing to do is to take pencil and paper and do the loop by hand

If you take size == 10 then size - 1 = 9

If i == 9 (this means: in the first iteration of the loop):
temp = array[i] = 8 (for example, if 8 was the 10-th value)
array[i] = array[i-1] = 7 (for example, if 7 was the 9-th value)
array[0] = temp = 8

This means that array[0] is overwritten before its value is temporarily stored.
Take a pencil and do the loop by hand (write down the value of each variable). You will find out what's going wrong.

4. To make it a bit more clear: suppose for simplicity that ARRAY_SIZE is 5 and the initial array is {1, 2, 3, 4, 5}

Then after the first iteration you have the following array:

Code:
`{5, 2, 3, 4, 4}`
You see, the 1 is gone. And the 4 on the fourth position is also still there.

5. thank u I did it!

6. It's easier, cleaner, more convenient, using two arrays; but requires more memory.

In pseudo;

A[3] = {1, 2, 3}
B[3]
N = 3
ShiftLen = 1

For k = 0, N-1
B[k] = A[(k + ShiftLen)%N];

---

ShiftLen = 0; B = { 1, 2, 3}
ShiftLen = 1; B = { 2, 3, 1}
ShiftLen = 2; B = { 3, 1, 2}
ShiftLen = 3; B = { 1, 2, 3}

---

I've had to implement a continuously rotating array for a visual effect I was developing, and the rate of rotations changed based on events. For that I just coupled the arrays together like such; A[2][N]. I would use a variable mod 2 to flip-flop the array being referenced. For rotations one iteration would be A[0][N] = A[1][shift % N], the next iteration A[1][N] = A[0][shift % N].

I know how awkward that seems at first, but it's fast and drastically easier to work with. Going the way without coupled arrays you're going to have to create loops that partition the array in to N-size segment loops, and then you have to deal with funky 'boundary' conditions so everything wraps correctly around the end/beginning (which means a host of crappy tmp vars and unintuitive tricks).

Sacrifice memory for agility, sometimes it is freaking worth it.... of course it also depends on your application.