As a C++ professional programmer, you might often come across this challenge during an interview case or even a programming solution challenge. Therefore, we must equip ourselves with this knowledge of how to code a hollow diamond pattern in C++.
The joy and motivation of programming are seen in the results. It’s very frustrating when your code does not run, but things and passion grow when codes run smoothly. That does not mean that every code you write will run perfectly. Sometimes, it needs an extra push!
That said, let’s now jump to writing a hollow diamond program in C++. To achieve this, we will really need to borrow the knowledge of loops.
What are loops?
Loops are programs that iterate several times until a specific condition is met. In our case, we will use the loops to iterate through the program until we meet our goal of a hollow diamond pattern.
There are three types of loops, i.e., For loop, while loop, and do-while loop. We use a “for loop” when we know the specific number of times we need to Iterate through a block of code, a “while loop” when we are not sure of the number of times, we need to loop through a code block and a “do-while loop” when we have to execute the loop at least once.
We will try to apply these three loops, if-else statements, and nested for loops and write a code in print a hollow diamond pattern. A nested loop is a loop inside another loop. For the outer loop to execute, the inner loop must execute completely.
Let’s first write a code to print a hollow diamond and then later we print the diamond pattern.
Hollow diamond in C++
#include <iostream> using namespace std; int main() { int x, p, k, mid; cout << "Enter number of lines to print the hollow diamond: "; cin >> x; if(x %2 == 1) { x++; } mid = (x/2); for(p = 1; p<= mid; p++) { for(k = 1; k<=(mid-p); k++) { cout << " "; } if(p == 1) { cout << "*"; }else{ cout << "*"; for(k = 1; k<=2*p-3; k++) { cout << " "; } cout << "*"; } cout << endl; } for(p = mid+1; p<x; p++) { for(k = 1; k<=p-mid; k++) { cout << " "; } if(p == x-1) { cout << "*"; }else{ cout << "*"; for(k = 1; k<=2*(x - p)-3; k++) { cout << " "; } cout << "*"; } cout << endl; } }
Output:
From the above code, we are used the “*” to print the hollow diamond. The diamond is divided into two parts. We have the upper half where we are increasing the space count and the lower half where we are decreasing the space count; hence, we want to print a hollow diamond pattern of 8 lines.
The upper half will contain four lines, and the lower half will contain four lines. The middle line is common at is counted on both ends. In the code, we started by allowing a user to enter the number of lines to print the diamond pattern. The above code uses the for loop and if-else statement.
Let’s write another code to achieve the same but this time using the while loop.
While loop Hollow diamond pattern in C++
#include <iostream> #include <conio.h> using namespace std; int main(){ int p,k,No_rows; cout<<"Enter the number of rows you wish t print the diamond pattern:"; cin>>No_rows; //code to print the top half part of the diamond p=1; while(p<=No_rows){ k=p; // code to printing the "*" while(k<=No_rows){ cout<<"*"; k++; } k=1; // code to printing the space " " while(k<=(2*p-2)){ cout<<" "; k++; } k=p; while(k<=No_rows){ cout<<"*"; k++; } cout<<"\n"; p++; } //code to print the bottom half part of the diamond p=1; while(p<=No_rows){ k=1; while(k<=p){ cout<<"*"; k++; } k=(2*p-2); while(k<(2*No_rows-2)){ cout<<" "; k++; } k=1; while(k<=p){ cout<<"*"; k++; } cout<<"\n"; p++; } getch(); return 0; }
Output:
Everything is coo so far, lets now make this a pattern and print a hollow diamond pattern.
For loop Hollow diamond pattern in C++
#include<iostream> using namespace std; void diamond(int p) { // the top half of the hollow diamond pattern including the code to print tp left triangle and the top right triangle for (int k = 0; k < p; k++) { for (int j = 0; j < (2 * p); j++) { if (k + j <= p - 1) cout << "*"; else cout << " "; if ((k + p) <= j) cout << "*"; else cout << " "; } cout << "\n"; } // the bottom half of the pattern including the code to print bottom left triangle and right triangle for (int k = 0; k < p; k++) { for (int j = 0; j < (2 * p); j++) { if (k >= j) cout << "*"; else cout << " "; if (k >= ((2 * p) - 1) - j) cout << "*"; else cout << " "; } cout << "\n"; } } int main() { int Number_of_rows; cout << "Enter number of rows to print in the hollow diamond pattern :"; cin >> Number_of_rows; diamond(Number_of_rows); return 0; }
Output:
This last code has taken a scenario where the diamond hollow pattern has four triangles rather than the upper half and lower half as done previously. In that case, we have used the “diamond ()” method. The first for loop is for the left triangle and the upper right triangle standing for the top half of the pattern, while the second loop is for the bottom left triangle and the bottom right triangle. We then write the main () method to allow user inputs on the number of rows.
Conclusion
As we all have different levels of programming skills, different ways of writing the programming code, we have noticed that we can approach this differently and still achieve the same results. It all depends on the way you are going to approach the problem at hand.
In this tutorial, we have covered how to program a hollow diamond pattern. I hope this helps you improve your programming experience. I suggest you learn this, understand, and try to come with a numbered how diamond pattern rather than using the “*”.