(Square of Asterisks) Write a program that reads in the size of the side of a square then prints a hollow square of that size out of asterisks and blanks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 5, it should print

*****
* *
* *
* *
*****
What will be an ideal response?

```
// Displaying a square of asterisks.
#include
using namespace std;

int main()
{
int stars; // number of stars on a side
int column; // the current column of the square being printed
int row = 1; // the current row of the square being printed

// prompt and read the length of the side from the user
cout << "Enter length of side:";
cin >> stars;

// valid input, if invalid, set to default
if ( stars < 1 )
{
stars = 1;
cout << "Invalid Input\nUsing default value 1\n";
} // end if
else if ( stars > 20 )
{
stars = 20;
cout << "Invalid Input\nUsing default value 20\n";
} // end else if

// repeat for as many rows as the user entered
while ( row <= stars )
{
column = 1;

// and for as many columns as rows
while ( column <= stars )
{
if ( row == 1 )
cout << "*";
else if ( row == stars )
cout << "*";
else if ( column == 1 )
cout << "*";
else if ( column == stars )
cout << "*";
else
cout << " ";

column++; // increment column
} // end inner while

cout << endl;
row++; // increment row
} // end outer while
} // end main
```

Computer Science & Information Technology

You might also like to view...

The loop created by the For...Next statement is a ____ loop.

A. posttest B. pretest C. selection D. sequential

Computer Science & Information Technology

The command show ip route is used on a router to do what? (Select all that apply.)

a. Set a static route b. Configure a static route c. Display the configured routes on a router d. Display how often routing updates are sent

Computer Science & Information Technology