C41 Answer


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

/* Puzzle C41 -- swap the top half of an array with the bottom half
|
|  Within each half, keep the elements in their original order.
|  If the array has an odd number of elements, the middle element
|  remains where it is.
*/
void swapHalves( int arr[], int size )
{
  int j, temp;
  for ( j=0; j<size/2; j++ )
  {
    temp = arr[j];
    arr[j] = arr[size/2+size%2+j];
    arr[size/2+size%2+j] = temp;
  }
}

void rotateRightArray( int arr[], int size )
{
  int j;
  int lastElt;
  
  lastElt = arr[size-1];
  for ( j=size-1; j>=1; j-- )
      arr[j] = arr[j-1];
      
  arr[0] = lastElt;
}

void rotateRightNArray( int arr[], int size, int N )
{
  int j;
  
  /* Adjust N */
  if ( N<0 )
  {
    N = -N ;
    N = N%size;
    N = size - N;
  }
  else
    N = N%size;
  
  for ( j=0; j<N; j++ )
    rotateRightArray( arr, size );
}

/* Obvious solution, but wrong
|
|  This works great when size is even. But when size is odd,
|  the middle element is moved, contrary to specification.
|
*/
void swapHalvesWrong( int arr[], int size )
{
    rotateRightNArray( arr, size, size/2 );
}

void fillArrayInOrder( int arr[], int size )
{
  int j;
  
  for ( j=0; j<size; j++ )
  {
    arr[j] = j;
  }
}

void printArray( int arr[], int size )
{
  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(int argc, char *argv[])
{
  const int SIZE = 19;
  int x[ SIZE ];
  int shift = 10;
  
  printf("Original:\n");
  fillArrayInOrder( x, SIZE );
  printArray( x, SIZE );
  printf("\nSwap Halves (correct version): \n");
  swapHalves( x, SIZE );
  printArray( x, SIZE );

  fillArrayInOrder( x, SIZE );
  printf("\nSwap Halves (incorrect version): \n");
  swapHalvesWrong( x, SIZE );
  printArray( x, SIZE );

  printf("\n\n");
  system("PAUSE");	
  return 0;
}