I05 Answer


/* Puzzle I05 -- checkerboard
|
|
*/
const int levelA = 100, levelB = 200, levelOutside = 50;

makeChecker( FILE *image, int nrows, int ncols, int numDown, int numAcross)
{
  int r, c; /* row and col of current pixel */
  int checkSizeRow, checkSizeCol ; /* Size of squares in pixels */
  int ckrow, ckcol; /* row and col in terms of the squares */
  int outside; /* if current pixel is outside of the board */

  checkSizeRow = nrows/numDown;
  checkSizeCol = ncols/numAcross;

  /* write out the pixel data */
  for ( r=0; r < nrows; r++ )
  {
    outside = 0 ;
    ckrow = r/checkSizeRow ;
    if ( ckrow == numDown ) outside = 1;

    for ( c=0; c < ncols; c++ )
    {
      ckcol = c/checkSizeCol ;
      if ( ckcol == numAcross ) outside = 1;

      if ( outside )
        fputc( levelOutside, image );
      else if ( (ckcol+ckrow)%2 == 1 )
        fputc( levelA, image );
      else
        fputc( levelB, image );
    }
  }
}

back