Answer 2D15

int rotate ( int nrows, int ncols, int x[nrows][ncols], int N )
{
  int temp[N];
  int j ;

  /* If N is out of range, return 0 */
  if ( N < 0 || N > nrows*nrows) return 0;

  /* Save the first N entries */
  for ( j=0; j<N; j++ ) temp[j] = x[0][j];

  /* Slide elements down */
  for ( j=0; j<nrows*ncols-N; j++ )
    x[0][j] = x[0][j+N];

  /* Copy first N entries to last N positions */
  for ( j=0; j<N; j++ )
    x[0][nrows*ncols-N+j] = temp[j];

  return 1;
}


Back to Puzzle Home