On writing programs to generate patterns

What kind of patterns are we talking about?

The ones that appear on exercises, exams or other places to test basic programming skills.

We shall take a look at different kinds of patterns and try to understand the process with which one might come up with a solution that can generate such patterns.

Triangular patterns

Write a program to generate the following pattern:

*
**
***
****
*****

So, here’s a solution:

#include <stdio.h>

int main(void)
{
    printf("*\n**\n***\n****\n*****\n");
    return 0;
}

Obviously, it is not the solution one might have anticipated. It generates exactly what was asked, no more, no less.

The actual questions might look the the following:

Write a program to generate the following pattern, taking the number of lines to output as input from the user:

*
**
***
****
*****
Pattern 1: Left aligned, increasing, triangular star pattern

The key observation here is that, on the k-th line, k stars are printed. So, one might write the following:

#include <stdio.h>

int main(void)
{
    int h;
    printf("Enter the number of lines: ");
    scanf("%d", &h);
    for (int k = 1; k <= h; k++) {
        for (int i = 1; i <= k; i++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}

Now, let’s take a look at the pattern reflected along the horizontal axis:

*****
****
***
**
*
Pattern 2: Left aligned, decreasing, triangular star pattern

Here, given h as the number of lines, on the k-th line, there’s h - k + 1 stars. In the following code, we start the inner loop counter from zero so that that loop iterates one more time than h - k, as required.

#include <stdio.h>

int main(void)
{
    int h;
    printf("Enter the number of lines: ");
    scanf("%d", &h);
    for (int k = 1; k <= h; k++) {
        for (int i = 0; i <= h - k; i++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}

Then we look at the pattern rotated two right angles:

    *
   **
  ***
 ****
*****
Pattern 3: Right aligned, increasing, triangular star pattern

Here, we see that we have to use spaces before the stars. Here’s another important observation, given h as height, there’s h characters on each line: on the k-th line, there’s k stars and h - k spaces before the stars.

#include <stdio.h>

int main(void)
{
    int h;
    printf("Enter the number of lines: ");
    scanf("%d", &h);
    for (int k = 1; k <= h; k++) {
        for (int j = 1; j <= h - k; j++) {
            putchar(' ');
        }
        for (int i = 1; i <= k; i++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}

An interesting variation of the previous pattern can be obtained with adding a space after each star character:

        // ...
        for (int i = 1; i <= k; i++) {
            putchar('*');
            putchar(' ');
        }
        // ...

The pattern looks like the following:

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
Pattern 4: Center aligned, increasing, triangular, spaced star pattern

Then, there’s the pattern 3 reflected along the horizontal axis:

*****
 ****
  ***
   **
    *
Pattern 5: Right aligned, decreasing, triangular star pattern

Again, we observe that, given h as height, each line consists of h characters: On k-th line, there’s k - 1 space characters, h - (k - 1) = h - k + 1 star characters.

In the following implementation, we use less than in the condition so that that loop iterates k - 1 times.

#include <stdio.h>

int main(void)
{
    int h;
    printf("Enter the number of lines: ");
    scanf("%d", &h);
    for (int k = 1; k <= h; k++) {
        for (int j = 1; j < k; j++) {
            putchar(' ');
        }
        for (int i = 0; i <= h - k; i++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}

Similarly to pattern 4, we can obtain the following pattern:

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
Pattern 6: Center aligned, decreasing, triangular, spaced star pattern

Now, if we fill the gaps between stars in pattern 4, we get another pattern:

    *
   ***
  *****
 *******
*********

Here, given h as height, on k-th line, there’s h - k spaces and 2k - 1 stars.

#include <stdio.h>

int main(void)
{
    int h;
    printf("Enter the number of lines: ");
    scanf("%d", &h);
    for (int k = 1; k <= h; k++) {
        for (int j = 1; j <= h - k; j++) {
            putchar(' ');
        }
        for (int i = 1; i < 2 * k; i++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}