Problem statement:
You are given an integer n, representing the side length of a rhombus. Your task is to write a program to print the pattern of the sides and diagonals of the rhombus.
INPUT
The only line of the input contains a single integer n, representing the side length of a rhombus.
OUTPUT
Print the diagonals and sides of a rhombus.Sample Input: 5
Sample Output:* *** * * * * * * ********* * * * * * * *** *
I tried this code but I’m unable to print the vertical bar of Rhombus.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
// Upper half of the rhombus
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print asterisks and spaces
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == n) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
// Lower half of the rhombus
for (int i = n - 1; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print asterisks and spaces
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
5
A rhombus is a a quadrilateral all of whose sides have the same length.
The definition does not say anything about the angle sizes, so that may be varied, but I shall assume that its “top” will be in the middle and the sides open downward by increasing/decreasing their position by 1 each time. So you have three parts:
- first part
- middle section
- second part
Input is N
So the first question is: how long will be the middle section?
Since we have N lines in the first part, that means that we have 1 (the middle) + N (left) + N (right). Therefore the answer is 2N + 1
Hence, if we start coordinates from 0, then the middle is N characters away.
Let’s therefore draw the first half:
for (int i = 0; i < N; i++) {
int left = N - i;
int right = N + i;
for (int j = 0; j < 2 * N + 1; j++) {
cout << ((i == left) || (i == right) || (i == N)) ? '*' : ' ';
}
cout << endl;
}
Now, let’s draw the middle (this will be easy):
for (int i = 0; i < 2 * N + 1; i++) {
cout << '*';
}
cout << endl;
And let’s do it for the bottom too:
for (int i = 0; i < N; i++) {
int left = i;
int right = 2 * N - i;
for (int j = 0; j < 2 * N + 1; j++) {
cout << ((i == left) || (i == right) || (i == N)) ? '*' : ' ';
}
cout << endl;
}
Basically we are consistently displaying on the specified lines. Code was untested, please let me know if anything does not click.
If you index the rows and columns from -n+1 to n-1, then the stars appear when either coordinate is 0, or the sum or difference of the two coordinates is n-1 or -n+1. Yielding this relatively concise code:
#include <iostream>
using namespace std;
int main() {
int n = 8;
--n;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++) {
cout << ((i == 0 || j == 0 || abs(i - j) == n || abs(i + j) == n) ? "*" : " ");
}
cout << "n";
}
cout << "n";
}
The code uses a fixed n, but you can obviously change it to respond to input.
Using meaningful names for your variables would help. We can think of left
, mid
and right
, each representing a position for an asterisk on a line. The middle line will be exception to this, having a solid line.
A recursive solution comes to mind, where before and after each recursive call you print the same line. The base case is when left
is 1, and then a solid line is printed:
void line(int left, int mid, int right) {
for (int i = 1; i <= right; i++) {
std::cout << (left == 1 || i == left || i == mid || i == right ? '*' : ' ');
}
std::cout << "n";
}
void rhombus_slice(int left, int mid, int right) {
line(left, mid, right);
if (left <= 1) return;
rhombus_slice(left - 1, mid, right + 1);
line(left, mid, right);
}
int main() {
int n = 5;
rhombus_slice(n, n, n);
}
1