# C++: Programming Problem - Figures

Here are some classic programming problems that require the use of nested loops. A frequent textbook problem is to draw figures using characters. If you already know how to define functions, you might find it useful to write convenience functions to help you.

You're welcome to change the appearance of these, or produce something a little different. The point of this is to give you practice with loops and functions, so show what you can do.

1. Print a rectangle. Read a height and width and print a rectangle with that height and width. This example output has height 3 and width 7.

                 *******
                 *******
                 *******

   Sample solution.

       int width, height;

       while (cin >> width >> height) {
           for (int row=0; row<height; row++) {
               for (int col=0; col<width; col++) {
                   cout << "*";
               }
               cout << endl;  // end the line of stars.
           }
       }

2. Print a triangle. Read a number and print a triangle of that size. This example is of size 6.

                 *
                 **
                 ***
                 ****
                 *****
                 ******

   Here is a solution.

       int size;

       while (cin >> size) {
           for (int row=1; row<=size; row++) {
               for (int col=0; col<row; col++) {
                   cout << "*";
               }
               cout << endl;  // end the line.
           }
       }

3. Print a triangle. Read a number and print a triangle of that size. This is a little different than the problem above because you'll have to print some spaces at the beginning of each line. This example is of size 6.

                      *
                     **
                    ***
                   ****
                  *****
                 ******

4. Print a rectangle outline. Read a height and width and print a rectangle that looks like the one below. This example has a height of 5 and width of 7. You might choose one of two approaches to this problem.
   1. Break it down into three problems: a line of stars; lines consisting of a star, blanks, and a star; and again a line of stars.
   2. At each position write either a star or blank. Use an `if` statement to test for first or last row of first or last column, in which case a star is printed, otherwise print a blank.

                 *******
                 *     *
                 *     *
                 *     *
                 *******

5. Print a pyramid. Read (in a loop of course) a number, n, and print a pyramid that has that size. The example below shows what would be printed for n=4.

                   *
                  * *
                 * * *
                * * * *

6. Print a parallelogram. Again read n in a loop. For each value of n produce output that makes a parallelogram like the following for n=6. You might find it easier to solve this to breaking the problem into two simpler problems: eg, thinking of this as an upper triangle and a lower triangle.

                 *
                 **
                 ***
                 ****
                 *****
                 ******
                  *****
                   ****
                    ***
                     **
                      *