A27 Answer


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

/* Puzzle A27 -- print a checkerboard of stars and dots */
int main(int argc, char *argv[])
{
  int row, col;
  int boardsize=256, squaresize=32 ;
  
  for ( row=0; row<boardsize; row++ )
  {
    for ( col=0; col<boardsize; col++ )
    {
      if ( (row/squaresize + col/squaresize)%2 == 0 )
        printf("*");
      else
        printf(".");
    }
    printf("\n");
  }
  
  printf("\n");
  system("PAUSE");	
  return 0;
}

Comments: The checkerboard is divided into rows and columns of small squares. Think of the row number and column number of these small squares:

When a character is being printed at location (row, col) , the expression row/squaresize gives the row number of the small square that it is in and the expression col/squaresize gives the column number of that small square.

Notice that the sum of the row and column number of the small squares alternates between even and odd. So the evenness or oddness of the sum is used to pick the color of a square:

      
  if ( (row/squaresize + col/squaresize)%2 == 0 )
    printf("*");
  else
    printf(".");