/*****************************************************************************\ * Exercise 8-1: a program that prints a grid from user-entered dimensions * * Author: Suzi Anvin * * Purpose: practice using for statements in C \*****************************************************************************/ #include void draw_dash_plus_line (int width) { int i; /* counters */ int j; /* setting up loops to draw a grid of the specified width. */ for (i = 0; i < width; i++) { /* print height + 1 vertical lines that are width long */ putchar('+'); for (j = 0; j < 5; j++) { putchar('-'); } } putchar('+'); putchar('\n'); } void draw_pipe_space_lines (int width) { int i; /* counters */ int j; int k; for (i = 0; i < 3; i++) { for (j = 0; j < width; j++) { putchar('|'); for (k = 0; k < 5; k++) { putchar(' '); } } putchar('|'); putchar('\n'); } } int main() { char line[50]; int width = 0; /* user selected width of grid */ int height = 0; /* heigth of grid */ int i; /* counter */ /* getting user dimensions for grid */ for (;;) { printf("Enter the size of desired grid A x B : "); fgets(line, sizeof(line), stdin); 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_dash_plus_line (width); draw_pipe_space_lines (width); } draw_dash_plus_line (width); printf("Would you like to print another grid? (y/n): "); fgets(line, sizeof(line), stdin); if (line[0] == 'y') continue; else break; } return (0); }