D14 Answer



void rotate2D ( int x[][NUMCOLS], int nrows )
{
  int r, c, elmnt00;

  elmnt00 = x[0][0];
  for ( r=0; r<nrows; r++ )
    for ( c=0; c<NUMCOLS; c++ )
      if      ( c>=1 ) x[r][c-1] = x[r][c];
      else if ( r>=1 ) x[r-1][NUMCOLS-1] = x[r][c];

  x[nrows-1][NUMCOLS-1] = elmnt00 ;
}

Remark: Element x[0][0] is a special case. It has to be saved before the loops execute, ready to be used in the last statement.

Another solution is to regard the block of memory that stores the array as a linear sequence of cells and to realize that rotating the array in the above fashion is just advancing the contents in each cells forward. The value in cell zero goes to the end. Here is code that uses that approach:

void rotate2Dlinear (int x[][NUMCOLS], int nrows )
{
  int j;
  int *y = (int *)x;
  int elmnt00 = y[0];
  for ( j=0; j < nrows*NUMCOLS-1; j++ )
    y[j] = y[j+1];
  y[nrows*NUMCOLS-1] = elmnt00;
}

The statement

  int *y = (int *)x;