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++.
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++.
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
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;
}
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;
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.
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
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
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.