C31 Answer


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

/* Puzzle C31 -- reverse the elements in an array */

void reverseArray( int arr[], int size )
{
  int j;
  int temp;
  for ( j=0; j < size/2; j++ )
  {
    temp = arr[j];
    arr[j] = arr[size-j-1];
    arr[size-j-1] = temp;
  }
}

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 = 2;
  int x[ SIZE ];
  
  fillArrayInOrder( x, SIZE );
  printf("Original:\n");
  printArray( x, SIZE );
  printf("\nReversed:\n");
  reverseArray( x, SIZE );
  printArray( x, SIZE );
    
  printf("\n\n");
  system("PAUSE");	
  return 0;
}