Hacker Challenge: Filled/Hollow Diamond and Crown
Learn how to print hollow shapes.
We'll cover the following
Diamond shape
In this lesson, we will use nested loops or the helping function, printASymbolKTimes()
, to print filled and hollow shapes of diamond and crown patterns.
Let’s first start with the shape of the filled diamond.
Exercise 1 (Warm-up): Filled diamond
We want the following shape:
Implementation direction
The diamond shape has two segments the top triangle followed by the lower one.
The height
value represents the number of lines that we want to print. In each line, we need to print some number of spaces followed by some stars.
Think about the pattern of the two segments and answer the following questions.
Filled-diamond pattern
What should be the pattern of the upper part of the filled diamond?
Here’s the sample shape for the upper part for height = 5
:
....*
...***
..*****
.*******
*********
In general, how many lines do we need to print for the lower part of the diamond?
Here’s the sample shape for the lower part for height = 5
:
.*******
..*****
...***
....*
What should be the pattern of the lower part of the filled diamond?
Here’s the sample shape for the upper part for height = 5
:
.*******
..*****
...***
....*
Instruction: Write the complete code in the playground below for the filled diamond. First, make the code for segment 1, then write the code for segment 2.
In case you are stuck at any point, look at the solution given below the playground.
We’ll explain how to create a hollow diamond shape. After going through the implementation details of a hollow diamond, you can add the code for the
hDiamond()
function in the same playground below.
#include <iostream>using namespace std;void printASymbolKTimes(char symbol, const int k){for (int ln = 1; ln <= k; ln++)cout << symbol;}void filledDiamond(const int height, char symbol){// Add code here}void hDiamond(const int height, char symbol){// Add code here}int main() {int height = 10;cout << "Filled diamond of height: "<<height << endl;filledDiamond(height, '*');cout << endl;// Uncomment the code below after implementing the hDiamond() function/* cout << "Hollow diamond of height: "<<height << endl;hDiamond(height, '*');cout << endl; */return 0;}
Exercise 2: Hollow diamond
Now, let’s learn to create a hollow diamond shape. For that, let’s identify its pattern and look at the image below.
Implementation direction
Look closely there are four segments in the hollow diamond:
First segment: The first line
In the first line, we need to print height-1
spaces followed by a symbol *
in our case.
.........*
Second segment:
From ln=2
to the line before the mid of the diamond i.e., ln=height
, we print some spaces followed by a symbol and then again some spaces followed by the symbol.
Pattern for Segment 2
What should be the pattern for the second segment?
Third segment: The lower segment
Let’s imagine that this segment starts with a new line ln=1
. Now if you look closely again we have a similar scenario as segment 2 i.e., spaces followed by a symbol(in our case '*'
) followed by spaces again and then the required symbol (in our case '*'
).
Pattern for Segment 3
What should be the pattern for the third segment?
Fourth segment: The last line
Can you see its similarity in one of the previous three segments?
Segment 4: Find the pattern
What should be the pattern in segment 4 (the last line)?
Instruction: Write the complete code in the playground above. If you are stuck at any point, look at the hint below with two implementations. Even if you are done with the directions given above, you should look at the second implementation in the solution, which involves a bit different way of attacking the problem.
Crown shape
Let’s exercise for a similar more challenging shape where we need to print both filled and hollow crowns.
Exercise 1 (Warm up): Filled crown
Write a program that prints the following crown:
*..............*..............*
**............***............**
***..........*****..........***
****........*******........****
*****......*********......*****
******....***********....******
*******..*************..*******
*******************************
*******************************
*******************************
Note: '.'
is just for counting purposes. You may consider it as space.
Implementation direction
If you look at the figure carefully, you can see that it has 2 segments. Three cascaded triangles and the base as shown in the following.:
Segment 1:
*..............*..............*
**............***............**
***..........*****..........***
****........*******........****
*****......*********......*****
******....***********....******
*******..*************..*******
*******************************
Segment 2:
*******************************
*******************************
The height of the first segment is height
and the second segment (the base) is height/4
.
Segment 1: Upper part
Make the pattern table for segment 1 and write the code in the above playground (in the fCrown()
function). The number of symbols in each line at the beginning and at the end is the same. Similarly, the spaces after the first triangle and second triangle are exactly the same. So, we need to figure out three patterns with respect to each line, ln
.
Pattern for the filled crown.
What should be the pattern for the first segment with respect to the line number, ln
.
Instruction: Write the code for segment 1 in the above playground and test it. The first segment should get printed.
Segment 2: Printing the base
If you look carefully, all you need to figure out is the total number of characters printed in each line (they are all the same).
Pattern for filled crown.
What should be the pattern for the base of the crown?
Instruction: Print the pattern for segment 2 (by iterating height/4
times and printing each line with symbols with the pattern you figured out).
In case you are stuck at any point, look at the solution given below the code playground.
Next, we’ll explain how to create a hollow crown shape. After going through the implementation details of a hollow crown, you can add the code for the
hCrown()
function in the same playground below.
#include <iostream>using namespace std;void printASymbolKTimes(char symbol, const int k){for (int ln = 1; ln <= k; ln++)cout << symbol;}void filledCrown(const int height, char symbol){// write the code here}void hCrown(const int height, char symbol){// write the code here}int main() {int height = 10;cout << "Filled crown of height: "<<height << endl;filledCrown(height, '*');cout << endl;// Uncomment the code below after implementing the hCrown() function/* cout << "Hollow Crown of height "<<height << endl;hCrown(height, '*');cout << endl; */return 0;}
Exercise 2: Hollow crown
Write a program that prints the following crown:
*..............*..............*
**............*.*............**
*.*..........*...*..........*.*
*..*........*.....*........*..*
*...*......*.......*......*...*
*....*....*.........*....*....*
*.....*..*...........*..*.....*
*......**.............**......*
*.............................*
*******************************
Note: '.'
is just for counting purposes; you may consider it as space.
Implementation direction
If you look at the figure carefully, you can see that it has 2 segments: three cascaded triangles and the base, as shown below.
Segment 1: The first line
*..............*..............*
Segment 2: The middle portion of the crown
**............*.*............**
*.*..........*...*..........*.*
*..*........*.....*........*..*
*...*......*.......*......*...*
*....*....*.........*....*....*
*.....*..*...........*..*.....*
*......**.............**......*
Segment 3: The upper base made up of spaces
*.............................*
Segment 4: The lower base is made up of stars
*******************************
Segment 1: The first line
Write down the pattern of the first segment.
What should be the pattern for segment 1?
Segment 2: The upper part of the crown
Segment 2 will have height-1
lines. Each line ln
has a fine pattern, where ln
starts from 2.
- A star
- Followed by some space:
ln-2
- Followed by a star
- Followed by spaces:
2 * (height - line)
- Followed by a star
- Followed by spaces:
ln*2-1-2
- Followed by a star
- Followed by spaces:
2 * (height - line)
- Then a star
- Followed by spaces:
ln-2
- Then a star
Instruction: Write the code for the above pattern and test it in main.
Segment 3: The upper base
The third segment should run height / 4 - 1
times. In each line, it should print the following:
- A symbol.
4 * height - 1 - 2
spaces.- A symbol.
Segment 4: The lower base
This contains 4 * height - 1
number of '*'
s.
Instruction: Add the code in the playground and test it on multiple of 4 values.