# Thread: Shifting elements in an array in C

1. ## Shifting elements in an array in C

Hi,

Let's say I have an array of 10 elements. I want user to enter 9 numbers so that they fill arrays 0 to 8 (9 numbers).
Now I shift the arrays +1. so array[0] is now array[1] and so on.
Now I ask user to enter 10th number (fills array 0).

how can I do that??

Here's my code(it doesn't shift arrays and doesn't ask for 10th num)

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

int main()
{
int a[10];
int i;

printf("\nEnter 9 numbers:\n");

for(i=0;i<9;i++)
{
printf("%10d numbers remaining\n\n", 9-i);
scanf("%d", &a[i]);
}

printf( "%s%13s\n", "Element", "Value" );
for(i=0;i<10;i++)
{
printf("%7d%13d\n",i, a[i]);
}

}```

2. Anyone??

3. The thing about the shifting is to be careful not to overwrite something that will be shifted later. In this case, it means that you want to start from the end, i.e., shift a[8] to a[9], then shift a[7] to a[8], etc.

4. It's not that difficult. Try starting from the end of the array (item[9]), not from its beginning. Take item[8] and put it into item[9], take item[7] and put it into item[8], take item[6] and put it into item[7], and so on (you can use "for" loops).

EDIT: LaserLight has been faster than me!!! Grrrrrrr...

5. Tee hee.

That said, do you really need to shift in the first place? It would be simpler and more efficient to just read into a[1] to a[9], and then read into a[0]. You are not constrained to read into elements in order of array position.

6. Provided that the shift is needed, couldn't one use memmove() too?