(Drawing Patterns with Nested for Loops) Write a program that uses for statements to print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an ap- propriate number of blanks. Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops.]



What will be an ideal response?

```
// Create triangles of asterisks using nested for loops.
#include
using namespace std;
int main()
{
int row; // the row position
int column; // the column position
int space; // number of spaces to print

// first triangle
for ( row = 1; row <= 10; row++ )
{
for ( column = 1; column <= row; column++ )
cout << "*";

cout << endl;
} // end for

cout << endl;

// second triangle
for ( row = 10; row >= 1; row-- )
{
for ( column = 1; column <= row; column++ )
cout << "*";

cout << endl;
} // end for

cout << endl;

// third triangle
for ( row = 10; row >= 1; row-- )
{
for ( space = 10; space > row; space-- )
cout << " ";

for ( column = 1; column <= row; column++ )
cout << "*";

cout << endl;
} // end for

cout << endl;

// fourth triangle
for ( row = 10; row >= 1; row-- )
{
for ( space = 1; space < row; space++ )
cout << " ";
for ( column = 10; column >= row; column-- )
cout << "*";

cout << endl;
} // end for
} // end main
```

Computer Science & Information Technology

You might also like to view...

Audience members expect a presenter who is ____.

A. an expert B. very experienced using PowerPoint C. prepared, confident, and enthusiastic D. better than other presenters

Computer Science & Information Technology

Combining two or more selected cells into one cell is called _____ cells.

A. merging B. mixing C. combining D. spanning

Computer Science & Information Technology