Loading...

Introduction

Pattern printing is not only an engaging exercise in programming but also a fantastic way to strengthen your understanding of loops and control structures.

Understanding the Pattern

A full pyramid pattern consists of rows of stars arranged in a pyramid shape. Each row has an increasing number of stars. The pattern can be visualized as a series of steps, with each step representing a row of stars.

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

Algorithm

To print a full pyramid star pattern, we need to follow a systematic approach. Here's a step-by-step algorithm:

  1. Take input from the user for the number of rows in the pyramid.
  2. Run an outer loop from 1 to the number of rows to represent each row of the pyramid.
  3. Run an inner loop to print the spaces before the stars, decreasing from the total number of rows to the current row.
  4. Run another inner loop to print the stars in each row, increasing from 1 to twice the current row minus 1.
  5. Move to the next line after printing stars for each row.

Code Implementation

#include <iostream>

int main() {
    int height = 5;

    for (int i = 1; i <= height; ++i) {
        // Print spaces for alignment
        for (int s = 1; s <= height - i; ++s) {
            std::cout << " ";
        }

        // Print stars in the pyramid
        for (int j = 1; j <= 2 * i - 1; ++j) {
            std::cout << "*";
        }

        // Move to the next line after each row
        std::cout << std::endl;
    }

    return 0;
}

Output:

full_pyramid_star_pattern

Explanation

  1. The outer loop (for(int i = 1; i <= height; ++i)) iterates through each row, where i represents the current row number.
  2. The first inner loop (for (int s = 1; s <= height -1; ++s)) prints spaces to align the stars and create the pyramid shape. The number of spaces is equal to height - i.
  3. The second inner loop (for (int j = 1; j <= 2 * i - 1; ++j)) prints the stars in each row. The number of stars in each row is given by 2 * i - 1, ensuring the correct sequence.
  4. The std::endl is used to move to the next line after printing each row.

Complexity Analysis

Time Complexity:

  • The outer loop runs for height iterations.
  • The first inner loop runs for height - i iterations for each row.
  • The second inner loop runs for 2 * i - 1 iterations for each row.

So, the overall time complexity is the product of these three loops, resulting in O(height^2).

The time complexity of generating a full pyramid pattern using nested loops is O(n^2), where n is the height of the pyramid. This is because, each row, we have to print 2 * i -1 stars, and there are height rows. The nested loops contribute to a quadratic time complexity.

Space Complexity:

The space complexity is O(1) as the memory usage remains constant regardless of the input.

Understanding Inverted Pyramid Pattern

The inverted pyramid pattern is characterized by its shape resembling an upside-down pyramid. It is characterized by a series of rows, each containing a decreasing number of stars. Consider the following example with a height of 5.

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

Each row consists of stars, and the number of stars decreases as we move down the pyramid.

Code Implementation For Inverted Pyramid Pattern

#include <iostream>

int main() {
    int height = 5;

    for (int i = 1; i <= height; ++i) {
        // Print spaces for alignment
        for (int s = 1; s < i; ++s) {
            std::cout << " ";
        }

        // Print stars in the inverted pyramid
        for (int j = 1; j <= 2 * (height - i) + 1; ++j) {
            std::cout << "*";
        }

        // Move to the next line after each row
        std::cout << std::endl;
    }

    return 0;
}

inverted_full_pyramid_star_pattern

Explanation

  1. The outer loop (for (int i  = 1; i <= height; ++i)) iterates through each row, starting from 1 and going up to the specified height.
  2. The first inner loop (for (int s = 1; s < i; ++s)) prints spaces to align the stars, and the number of spaces is equal to i - 1.
  3. The second inner loop (for (int j = 1; j <= 2 * (height - i) + 1; ++j)) prints the stars in the inverted pyramid pattern. the number of stars in each row is calculated as 2 * (height - i) + 1.
  4. The std::endl is used to move to the next line after printing each row.

Complexity Analysis

Time Complexity: The time complexity of generating the inverted pyramid pattern is O(n^2), where n is the height of the pyramid. Both the outer and inner loops contribute to the quadratic time complexity.

Space Complexity: The space complexity is O(1), constant, as the amount of memory used does not depend on the input size. The memory required for each iteration remains constant.