#include <stdio.h> #include <stdlib.h> /* Puzzle A23 -- print a n by n square divided into quardrants of stars, dots, dollars, and ohs */ int main(int argc, char *argv[]) { int row, col; int n=15; for ( row=0; row<n; row++ ) { for ( col=0; col<n; col++ ) { if ( row<n/2 && col<n/2 ) printf("*"); else if ( row<n/2 ) printf("."); else if ( col<n/2 ) printf("$"); else printf("o"); } printf("\n"); } printf("\n"); system("PAUSE"); return 0; }
Comments:
The if
structure determines which quadrant
the current row
and col
belong in,
then prints the appropriate character.
When n is odd, it is unclear what to do. Two small squares will be larger than the other two. Probably this is OK. In a serious application you would have to think carefully.
Maybe, when n is odd, you could make all the small squares the same size but separate them with a space.