[E-7]
Write a function that moves each element of an
array to the next highest index.
Unlike a right shift, the value at the highest index
is put into the array at index 0.
The array contains the same values as it starts with,
but each value is in a new position.
Here is an example:
0 1 2 3 4 5 6 7 8 9 Rotated Right by One: 9 0 1 2 3 4 5 6 7 8
Here is a testing framework:
#include <stdio.h> #include <stdlib.h> /* Puzzle C34 -- rotate every array element one position to the right; */ /* element 0 gets the original last element. */ void rotateRightArray( int size, int arr[] ) { . . . . } void fillArrayInOrder( int size, int arr[] ) { int j; for ( j=0; j<size; j++ ) { arr[j] = j; } } void printArray( int size, int arr[] ) { const int N = 10; int j; for ( j=0; j<size; j++ ) { if ( j%N == N-1 ) printf("%4d\n", arr[j] ); else printf("%4d ", arr[j] ); } } int main() { const int SIZE = 10; int x[ SIZE ]; fillArrayInOrder( SIZE, x ); printArray( SIZE, x ); printf("\nRotated Right by One:\n"); rotateRightArray( SIZE, x ); printArray( SIZE, x ); printf("\n\n"); return 0; }