Hacker Challenge: Printing a Star Pattern
Learn to create a star pattern using nested loops.
We'll cover the following...
Printing a star using nested loops
In this lesson and in the next, we’ll try to think of two solutions to print the star pattern using nested loops. Further, we will discuss how to simplify our implementation. In the 2nd implementation, we’ll learn to avoid writing nested loops and use a helper function to print patterns.
Problem Statement
Write a program that takes height (has to be a multiple of 4) as input from the user and prints a star, as shown below. The following shape is of height 12:
*
***
*****
*****************
***************
*************
*************
***************
*****************
*****
***
*
Before we delve into the possible solutions to this problem, let’s first try and observe the pattern.
There can be two possible approaches to solve this problem.
Approach for solution 1: We can see four segments in our star pattern. When the height is h, each of the four segments has h/4 rows or lines (12/4 = 3). We can create the pattern by focusing on each segment and printing each individually.
Approach for solution 2: We can also view the star pattern in two triangles. Let’s call them the ‘upper’ and ‘lower’ triangles. We can create the pattern by focusing on each triangle and printing them.
In this lesson, we’ll only solve the problem using the first approach.
Dividing in segments approach
Let us look at the example output for height 12. You should spend a while analyzing the pattern in the following figure:
If we look at the number of asterisks, they are all odd. We can also see the pattern of the spaces (where space is being replaced by - so that we can count it quickly).
We’ll need the following variables:
Will the following two statements give the same answer?
int h_t = 3/4 * h;int h_t = 3 * h/4;
Yes
No
As an example, imagine the following:
Here’s the first idea for solving this star printing shape:
- Divide the output into 4 segments and write a loop for every segment separately and figure out the pattern with respect to each segment. The flow of the program should be something as shown below.
Now to solve this problem, we’ll need to observe the number of spaces and asterisks required in each line of each segment and fill in the missing code in each segment.
Segment 1
For the first segment, look at the following code:
Can you see the pattern?
Let’s look at the table below and the last entry of the pattern in terms of line #.
Segment 1
Line # | Space (' ') | Asterisks ('*') |
1 | h_t - 1 | 1 |
2 | h_t - 2 | 3 |
3 | h_t - 3 | 5 |
. | . | . |
. | . | . |
ln | h_t - ln | 2*ln-1 |
To print this pattern of spaces and asterisks, we’ll need a for loop for the total number of lines in a segment.
Inside it, we can use two separate for loops where one loop prints the number of spaces and the second one prints the asterisk(s) for each line.
So our code for the first segment would look like this:
Segment 2
To print the second segment, look at the following code: :
Let’s look at the table below.
Segment 2
Line # | Space (' ') | Asterisks ('*') |
1 | 0 | 2*h_t-1 |
2 | 1 | 2*h_t-3 |
3 | 2 | 2*h_t-5 |
. | . | . |
. | . | . |
ln | ln - 1 | 2*h_t-(2*ln-1) |
So our code for the second segment would be look like this:
The outer loop in line 2 will run () times. The first inner for loop in line 4 prints the number of spaces. The first iteration of the first inner for loop won’t print any space. The second for loop in line 6 prints 17 asterisk(s) calculated from the expression using 2 * h_t - (2 * lines - 1) for the same line and so on for all the lines in the segment.
Segment 3
To print the segment, look at the following code:
Let’s look at the table below.
Segment 3
Line # | Space (' ') | Asterisks ('*') |
1 | h_s - 1 | h + 1 |
2 | h_s - 2 | h + 3 |
3 | h_s - 3 | h + 5 |
. | . | . |
. | . | . |
ln | h_s - ln | h + (2 * ln - 1) |
So our code for the third segment would look like this:
Segment 4
To print the fourth segment, look at the following code:
Let’s look at the table below.
Segment 4
Line # | Space (' ') | Asterisks ('*') |
1 | h/2 + 0 or h_t*2/3 + 0 | 2*h_s - 1 |
2 | h/2 + 1 or h_t*2/3 + 1 | 2*h_s - 3 |
3 | h/2 + 2 or h_t*2/3 + 2 | 2*h_s - 5 |
. | . | . |
. | . | . |
ln | h/2 + (ln-1) or h_t*2/3 + (ln-1) | 2*h_s - (2*ln - 1) |
The code for the fourth segment would be:
Now that we know what to write let’s create, a function called printStar() and add our code inside it.
Click “Run” in the code editor below to see the output:
The above code prints each segment inside separate for loops where each loop, in turn, contains two for loops ( a total of 12 for loops).