/** * Program to print a pattern of stars and spaces as shown in homework * writeup. * * Input: positive integer giving number of rows. * * Output: error message if what was entered is not a positive number, * or the desired pattern of stars and spaces. */ #include #include void print_repeated_char(char c, int n); void print_row(int stars1, int spaces, int stars2); /* * Main program */ int main(void) { int size; printf("enter the size of the square (must be positive)\n"); if ((scanf("%d", &size) != 1) || (size <= 0) ) { printf("not a positive number\n"); return EXIT_FAILURE; } else { print_row(2*size+4,0,0); for (int row = 0; row < size; ++row) { print_row(2+row*2, 2, 2+(size*2-row*2-2)); } print_row(2*size+4,0,0); return EXIT_SUCCESS; } } /* * Print character 'c' n times. */ void print_repeated_char(char c, int n) { for (; n > 0; --n) { putchar(c); } } /* * Print row with 'stars1' stars, then 'spaces' spaces, then 'star2' stars. */ void print_row(int stars1, int spaces, int stars2) { print_repeated_char('*', stars1); print_repeated_char(' ', spaces); print_repeated_char('*', stars2); putchar('\n'); }