Problem Solving: Hollow Patterns

Learn how to print hollow shapes. We will first print the hollow rectangle and then several hollow rectangles.

In this lesson, we will use nested loops to print hollow shapes. So let’s start with a hollow rectangle.

Hollow rectangle

Let’s write a program that prints the following shape.

Height: 6
Width : 20 
********************
*                  *
*                  *
*                  *
*                  *
********************

Let’s make the pattern table first then we will analyze how to implement it.

Hollow Rectangle

Line #

Number of asterisks

Spaces

Number of asterisks

1

width

No space

-

2

1

width - 2

1

3

1

width - 2

1

.

.

.

.

.

.

.

.

ln-1

1

width - 2

1

ln

width

No space

-

If you look at the table, the picture can be break it down into three segments:

First segment: The first horizontal line represents the first segment.

//First line
********************

Second segment: Parallel vertical lines represent the second segment.

//Second line to second last line
*                  *
*                  *
*                  *
*                  *

Third segment: The last horizontal line represents the third segment.

//The last line
********************

Let’s make a h_Rectangle() function in which we will pass width, height and symbol as parameters.

Implementation details

First segment: To print the first segment, we will make a loop that prints the asterisks in the first horizontal line of the width size.

Second segment: Now we want to print the second segment in which we will make a nested for loop to print two asterisks with spaces in each line. The inner loop will print the two asterisks with width-2 spaces. The outer loop will move the cursor to the next line in each iteration.

Third segment: To print the third segment, we will make a loop that prints the asterisks in the last horizontal line of the width size.

Let’s write the complete function, and test it in main():

Let’s write a complete code below:

#include <iostream>
using namespace std;
void h_Rectangle(const int height, int width, char symbol)
{
//First segment: Print the first horizontal line of width *s
for (int i = 1; i <= width; i++)
cout << symbol;
cout<<endl;// Move to the second line
//Second segment: '*' followed by width-2 spaces followed by '*'
for (int ln = 2; ln <= height-1; ln++)
{ //Run the loop from second line to second last line
cout << symbol;
for (int ln = 1; ln <= width-2; ln++)
cout << ' ';
cout << symbol<<endl;
}
//Third Segment: Print the horizontal last line of width *s
for (int i = 1; i <= width; i++)
cout << symbol;
}
int main()
{
h_Rectangle(4,20,'*');
return 0;
}
Hollow rectangle using nested loops

Let’s now move to some exercise problems for hollow triangles.

Exercise: Hollow triangles

Make the following hollow shapes:

Height: 7
Symbol: *

*       *******  *******        *
**      *    *    *    *       **
* *     *   *      *   *      * *
*  *    *  *        *  *     *  *
*   *   * *          * *    *   * 
*    *  **            **   *    *    
******* *              *  *******
 (A)    (B)       (C)      (D)

You need to work in the following playground.

Playground for your implementation

First, make the table pattern and then write the code here.

Press + to interact
#include <iostream>
using namespace std;
void printASymbolKTimes(char symbol, const int k)
{
for (int i = 1; i <= k; i++)
cout << symbol;
}
void shapeA(int h, char symbol)
{
// write code here
}
void shapeB(int h, char symbol)
{
// write code here
}
void shapeC(int h, char symbol)
{
// write code here
}
void shapeD(int h, char symbol)
{
// write code here
}
int main()
{
// your code goes here
int h = 12;
cout<< "Shape A"<<endl;
shapeA(h, '*');
cout<<endl;
cout<< "Shape B"<<endl;
shapeB(h, '*');
cout<<endl;
cout<< "Shape C"<<endl;
shapeC(h, '*');
cout<<endl;
cout<< "Shape D"<<endl;
shapeD(h, '*');
cout<<endl;
return 0;
}

Every triangle will have three segments, the top line containing either one star (for shapes A and D) or h stars (for shapes B and C). Similar in the last line. Then the middle segment should contain a pattern based on the line number. First, it should print a symbol, then spaces, and then a symbol again.