/** * diamond - Make and print a simple diamond from asterisks. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * print_n_copies (n, ch) * print n copies of the given character */ void print_n_copies (int n, char ch) { int i; // Counter variable. for (i = 0; i < n; i++) putchar (ch); } // print_n_copies /** * print_row (row, side) * prints the given row of a diamond with a partiuclar side * length. */ void print_row (int row, int side) { // Print the spaces in the row print_n_copies (side-1-row, ' '); // Print the asterisks print_n_copies (2*row_1, '*'); // That's it for the line printf ("\n"); } // print_row // +------+----------------------------------------------------------- // | Main | // +------+ int main (int argc, char *argv[]) { int side; // The length of the side int row; // A count of the rows we're printing // Verify that there are enough arguments if (argc != 2) { printf ("Usage: diamond SIDE\n"); return 1; } // Determine the side length side = atoi (argv[1]); // Validate the side legth if (side <= 0) { printf ("Invalid side length: %s\n", argv[1]); return 2; } // Print the top half for (row = 0; row < side; row++) print_row (row, side); // Print the bottom half for (row = side-2; row >= 0; row--) print_row (row, side); // And we're done return 0; } // main