Answer DD31

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

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

void reverseArray( int size, int arr[] )
{
  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 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 = 21;  /* change this to 1.  Does the program still work? */
  int x[ SIZE ];
  
  fillArrayInOrder( SIZE, x );
  printf("Original:\n");
  printArray( SIZE, x );
  printf("\nReversed:\n");
  reverseArray( SIZE, x );
  printArray( SIZE, x );
    
  printf("\n\n");
  return 0;
}


Back to Puzzle Home