Hacker Challenge: Printing a Star Pattern

Learn to create a star pattern using nested loops.

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.

Press + to interact
Approach 1
Approach 1

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.

Press + to interact
Approach 2
Approach 2

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:

Press + to interact
Height: 12
--------* //8 spaces and 1 asterisk
-------*** //7 spaces and 3 asterisks
------***** //6 spaces and 5 asterisks
***************** //0 spaces and 17 asterisks
-*************** //1 space and 15 asterisks
--************* //2 spaces and 13 asterisks
--************* //2 spaces and 13 asterisks
-*************** //1 space and 15 asterisks
***************** //0 space and 17 asterisks
------***** //6 spaces and 5 asterisks
-------*** //7 spaces and 3 asterisks
--------* //8 spaces and 1 asterisk

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:

Press + to interact
//height of the star
int h = height;
//height of each segment
int h_s = h/4;
//height of upper and lower triangles
int h_t = 3*h/4

Will the following two statements give the same answer?

  1. int h_t = 3/4 * h;
  2. int h_t = 3 * h/4;
Q
A)

Yes

B)

No

As an example, imagine the following:

Press + to interact
h = 12; // This is the entire height of the star.
h_t = 9; // This is the height of both triangles i.e. 3.0/4 * h.
h_s = 3; // This is the height of every segment
i.e. 1.0/4 * h

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.
Press + to interact
// Printing segment 1
for(int ln = 1; ln<=h_s; ln++)
{
// code to print the first pattern
cout << endl;
}
// Printing segment 2
for (int ln = 1; ln <= h_s; ln++)
{
// code to print the second pattern
cout << endl;
}
// Printing segment 3
for (int ln = 1; ln <= h_s; ln++)
{
// code to print the third pattern
cout << endl;
}
// Printing segment 4
for (int ln = 1; ln <= h_s; ln++)
{
// code to print the fourth pattern
cout << endl;
}

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:

Press + to interact
--------* //h_t-1(i.e. 8) spaces and 1 asterisk
-------*** //h_t-2(i.e. 7) spaces and 3 asterisks
------***** //h_t-3(i.e. 6) spaces and 5 asterisks

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:

Press + to interact
//Segment 1: outer loop for each line
for(int ln = 1; ln<=h_s; ln++)
{
// loop to print spaces
for (int sp = 1; sp <= h_t - ln; sp++)
cout << " ";
// loop to print symbol
for (int symb = 1; symb <=2 * ln-1; symb++)
cout << "*";
cout << endl;
}

Segment 2

To print the second segment, look at the following code: :

Press + to interact
***************** //0 space and 17 asterisks
-*************** //1 space and 15 asterisks
--************* //2 space and 13 asterisks

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:

Press + to interact
for (int ln = 1; ln <= h_s; ln++)
{
for (int sp = 1; sp <= ln - 1; sp++)
cout << " ";
for (int sym = 1; sym <= 2 * h_t - (2 * ln - 1); sym++)
cout << "*";
cout << endl;
}

The outer loop in line 2 will run (h/4h/4) 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:

Press + to interact
--************* //2 space and 13 asterisks
-*************** //1 space and 15 asterisks
***************** //0 space and 17 asterisks

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:

Press + to interact
for (int ln = 1; ln <= h_s; ln++)
{
for (int sp = 1; sp <= h_s - ln; sp++)
cout << " ";
for (int symb = 1; symb <= h + (2 * ln - 1); symb++)
cout << "*";
cout << endl;
}

Segment 4

To print the fourth segment, look at the following code:

Press + to interact
------***** //6 spaces and 5 asterisks
-------*** //7 spaces and 3 asterisks
--------* //8 spaces and 1 asterisk

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:

Press + to interact
for (int ln = 1; ln <= h_s; ln++)
{ // or sp<= h_t*2/3 + (ln-1)
for (int sp = 1; sp <= h/2 + (ln-1); sp++)
cout << " ";
for (int sym = 1; sym <= 2*h_s - (2 * ln - 1); sym++)
cout << "*";
cout << endl;
}

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:

Press + to interact
#include <iostream>
using namespace std;
void printStar(const int height)
{
int h = height;
int h_s = h / 4; // Section height
int h_t = 3* (h / 4) ; // Triangle height
// Printing Section 1
for(int ln = 1; ln<=h_s; ln++)
{
for (int sp = 1; sp <= h_t - ln; sp++)
cout << " ";
for (int symb = 1; symb <=2 * ln-1; symb++)
cout << "*";
cout << endl;
}
// Printing Section 2
for (int ln = 1; ln <= h_s; ln++)
{
for (int sp = 1; sp <= ln - 1; sp++)
cout << " ";
for (int sym = 1; sym <= 2 * h_t - (2 * ln - 1); sym++)
cout << "*";
cout << endl;
}
// Printing Section 3
for (int ln = 1; ln <= h_s; ln++)
{
for (int sp = 1; sp <= h_s - ln; sp++)
cout << " ";
for (int symb = 1; symb <= h + (2 * ln - 1); symb++)
cout << "*";
cout << endl;
}
// Printing Section 4
for (int ln = 1; ln <= h_s; ln++)
{ // or sp<= h_t*2/3 + (ln-1)
for (int sp = 1; sp <= h/2 + (ln-1); sp++)
cout << " ";
for (int sym = 1; sym <= 2*h_s - (2 * ln - 1); sym++)
cout << "*";
cout << endl;
}
}
int main()
{
printStar(12);
return 0;
}

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