/*****************************************************************************\ * Exercise 8-1: a program that prints a grid from user-entered dimensions * * Author: Suzi Anvin * * Purpose: practice using for statements in C \*****************************************************************************/ #include /* setting up loops to draw a line with 'width' groups of spacers * separated by edges */ void draw_line (int width, char edge, char spacer) { int i, j; /* counters */ for (i = 0; i < width; i++) { putchar(edge); for (j = 0; j < 5; j++) { putchar(spacer); } } putchar(edge); putchar('\n'); } int main() { char line[50]; int width = 0; /* user selected width of grid */ int height = 0; /* heigth of grid */ int i, j; /* counters */ /* getting user dimensions for grid */ for (;;) { printf("Enter the size of desired grid (A x B) or q to quit : "); fgets(line, sizeof(line), stdin); if (line[0] == 'q' || line[0] == 'Q') break; sscanf(line, "%d %*[^-0-9] %d", &width, &height); /* limiting grid size */ if (width > 12 || width <= 0 || height > 12 || height <= 0 ) { printf("Please enter positive values of 12 or smaller.\n"); continue; /* re-input dimensions */ } /* setting up the loop to draw a grid of the specified height, * avoiding the fencepost error by using height + 1 */ for (i = 0; i < height; i++) { draw_line (width, '+', '-'); for (j = 0; j < 3; j++) { draw_line (width, '|', ' '); } } draw_line (width, '+', '-'); } return (0); }