C++ Reverse Half Pyramid Pattern Using Characters

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to print a Reverse Half Pyramid Structure using Characters in C++. This pattern can be generated by making use of the nested loop structures in C++.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/output -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/comments -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/variables -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/user_input -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/data_types -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/operators -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/for_loop -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} cpp/function_parameters -.-> lab-96221{{"`C++ Reverse Half Pyramid Pattern Using Characters`"}} end

Create a new C++ source file

First, let's create a new C++ source file. Open the terminal and navigate to the ~/project directory. Create a new C++ source file named main.cpp by using the following command:

cd ~/project
touch main.cpp

Write the main function

The first step in our program is to write the main function. This function will contain all other functions and data structures we will use in our program. We start by including the iostream library, which is used for input/output operations.

#include <iostream>

int main() {
    // Code goes here
    return 0;
}

Ask user for the number of rows

The next step is to ask the user for the number of rows in the pyramid. We can use the cin and cout functions to do this.

std::cout << "Enter the number of rows in the pyramid: ";
int rows;
std::cin >> rows;

Generate the pattern using nested loops

Now, let's generate the Reverse Half Pyramid Pattern using Characters. The approach here is to use nested loops. The outer loop will iterate through the rows of the pyramid, and the inner loop will iterate through each column of that row. Here's the code for generating the pattern.

char c, first, last; // defining variables
for(int i = rows; i >= 1; i--) { // the outer loop
    first = 'A';
    last = first + i - 1; // define last element
    c = 'A'; //  variable `c` is defined to `A`
    for(int j = 1; j <= i; j++) { // the inner loop
        std::cout << c << " "; // print character and empty space
        c++; // shift to next character
    }
    std::cout << std::endl;
}

In this code block, the code in the nested loop is used to determine the number of characters in a particular row. It starts from c = 'A' and keeps increasing until it reaches the last character for that particular row.

Compile and run the program

Once you have written the program, compile it using the following command:

g++ main.cpp -o main && ./main

If there are no errors in your program, the output should be as follows:

Enter the number of rows in the pyramid: 6
F F F F F F
E E E E E
D D D D
C C C
B B
A

Verify the output

To verify the output, check if the output generated by the program is correct by comparing it with the expected output. In this case, the expected output is as follows:

Enter the number of rows in the pyramid: 6

The required Reverse Pyramid pattern containing 6 rows is:

Row ## 1 contains characters from A to F : F F F F F F
Row ## 2 contains characters from A to E : E E E E E
Row ## 3 contains characters from A to D : D D D D
Row ## 4 contains characters from A to C : C C C
Row ## 5 contains characters from A to B : B B
Row ## 6 contains characters from A to A : A

Summary

In this lab, we learned how to print a Reverse Half Pyramid Structure using Characters in C++. We learned how to use nested loops to achieve this pattern, and we obtained an understanding of how to navigate the nested structure. By practicing these patterns, you'll become more familiar with nested loops in programming.

Other C++ Tutorials you may like