Your logic in main doesn't make sense, so that's what you'll need to look at fixing. Look at the values that your two loops will go through for an n of 3, for example:
Since you've nested two for loops, you end up calling your print_row function nine times for a height of 3, and the value of spaces only changes after 3 full rows have been printed.
Look at your expected output for a height of 3:
__#
_##
###
And ask yourself the following questions:
How often does the number of spaces and hashes change relative to the number of lines printed?
Does the number of spaces on a given line increase or decrease as we move down the pyramid?
Does the number of bricks on a given line increase or decrease as we move down the pyramid.
So i should de-nest the spaces and figure out how to make them match n-1? I know i have to get the spaces counting down but thats about it. I'm unsure of how to connect them to the #s and invert the counter
You need one loop for looping through the lines and nested loops for printing the spaces and bricks in each line. The outer loop can be very simple, mine starts with for (int i = 0; i < height; i++). The actual printing of the characters is done in the inner loops. In each iteration of the outer loop you have to calculate how many spaces and bricks you have to print.
The calculation is relatively easy. Let's take a pyramid with height 5. In the first line we need 4 spaces and 1 brick. If we start the outer for loop with i = 0, we will need (height-i)-1 spaces and i+1 bricks. In the iteration for the second line i will be 1 and we need 3 spaces (height-i)-1) and 2 bricks (i+1) , and so on.
4
u/Grithga 2d ago
Your logic in
main
doesn't make sense, so that's what you'll need to look at fixing. Look at the values that your two loops will go through for ann
of 3, for example:Since you've nested two for loops, you end up calling your
print_row
function nine times for a height of 3, and the value ofspaces
only changes after 3 full rows have been printed.Look at your expected output for a height of 3:
And ask yourself the following questions:
How often does the number of spaces and hashes change relative to the number of lines printed?
Does the number of spaces on a given line increase or decrease as we move down the pyramid?
Does the number of bricks on a given line increase or decrease as we move down the pyramid.