asterisk diamond

im creating a C++ program to make an asterisk diamond (*) based on the input of the user. Eventually i would have to use a function but aside form that, im having trouble setting up the for loops for this. Lets say the user inputs 4, i would need to have 3 spaces (" ") and a * for the first line, 2 spaces and 3 stars for the 2nd line, and the spaces keep decreasing while stars keep increasing until it hits line 4 (to which it will have no spaces an 7 stars) then it will inverse so that the 7th line will resemble the 1st, please someone help. Basically ( ---*
--***
-*****
*******
-*****
--***
---* ) if the user inputs 4

Im working on the first half (before the diamond inverses) and heres what i got

int main() {
int input;

cout << "Enter input: ";
cin >> input;

for (int i = 0; i < input; i++) {

for (int j = input - 1; j > 0; j--) {
cout << " ";
}
for (int k = 0; k < (input * 2) -1; k++) {
cout << "*";
}
cout << endl;
}
}

the output is
---*******
---*******
---*******
---*******

How come the program does not acumulate like i want it to?
Last edited on
Get a piece of paper and a pencil and walk through your code on paper keep track of the current state of your variables at each step and draw exactly what your code says it should. You should see as you walk through your code at what point you see that that's not what I want it to output. Then go back in your code to that point and try to find a fix.

Also, dont forget your return statement.

Another suggestion. Last time I helped someone with this I believe they ended up with 3 for loops each with 2 nested for loops. That doesn't mean that's how yours will be theirs always more than one way to solve a problem. Try walking through your code and if you're still stuck I can give you part of the code to set you in the right direction, but you learn best solving the problem yourself.
I have some kind of idea. I know the # of spaces starts off with (input -1) and each line it decrements, and the stars start off as 1 and increments by 2 each line until it reaches (2 * input) -1.

First for loop sets the rows
Second one establishes the amount of spaces on each row
And the Third does the same for stars

I realize that these for loops dont output each decrement/increment it prints out the whole command until i or j reaches its limit (which is "invisible" to the user). I need to find a way where it outputs those decriments.

Im guessing another for loop within each, im still boggled though and i really want (need) to figure this out myself, any advise for the code itself?
Divide and conquer

like @joe864864 said you need to first draw the problem and solve

divide the problem into small parts (cut the diamond horizontally and vertically maybe?)

I have some kind of idea. I know the # of spaces starts off with (input -1) and each line it decrements, and the stars start off as 1 and increments by 2 each line until it reaches (2 * input) -1.

write a pseudo code for that to get started ...

HOPE IT HELPS...
Try to think of this in smaller parts. Isn't a diamond just 2 pyramids/triangles? Instead of building the entire diamond try just building one pyramid. Then the other side of the diamond is just the first pyramid backward.

Also, I don't know why I said 3 for loops earlier should only need 2.
Topic archived. No new replies allowed.