(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...

Variable names cannot contain ____.

A. numbers B. spaces C. underscore characters D. any of the above

Computer Science & Information Technology

The object element can insert any type of content into a Web page, even images or another XHTML document

Indicate whether the statement is true or false

Computer Science & Information Technology

The algorithm ____ determines whether the elements in one range appear in another range.

A. includes B. contains C. has D. associates

Computer Science & Information Technology

?A(n) _________ style sheet is a text file with the .css file extension.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology