Puzzle DD36


Rotate array elements one position left

[E-7] Write function that rotates array elements left by one position. The value originally in slot 0 is moved to slot n-1.

  77    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19
  20   21   22   23   24
  
Rotated Left:
   1    2    3    4    5    6    7    8    9   10
  11   12   13   14   15   16   17   18   19   20
  21   22   23   24   77

Here is a testing framework:

#include <stdio.h>
#include <stdlib.h>

/* Puzzle C36 -- rotate array elements one position to the left */
void rotateLeftArray( 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 = 25;
  int x[ SIZE ];
  
  fillArrayInOrder( SIZE, x );
  x[0] = 77;
  printArray( SIZE, x );
  printf("\nRotated Left:\n");
  rotateLeftArray( SIZE, x );
  printArray( SIZE, x );
    
  printf("\n\n");
  return 0;
}



Answer         Next Page         Previous Page Home