/** * diamond - Make and print a simple diamond from asterisks. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include // +------+----------------------------------------------------------- // | Main | // +------+ int main (int argc, char *argv[]) { int side; // The length of the side int row; // A count of the rows we're printing int i; // A counter variable // 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 the spaces in the row for (i = 0; i < side-1-row; i++) putchar (' '); // Print the asterisks for (i = 0; i < 2*row+1; i++) putchar ('*'); // That's it for the line printf ("\n"); } // for lines in top half // Print the bottom half for (row = side-2; row >= 0; row--) { // Print the spaces in the row for (i = 0; i < side-1-row; i++) putchar (' '); // Print the asterisks for (i = 0; i < 2*row+1; i++) putchar ('*'); // That's it for the line printf ("\n"); } // for lines in bottom half // And we're done return 0; } // main