So after fiddling with this for about 2 days, I managed to get a working code that gives me the result I wanted, but the last part was pure luck and frustration as I entered random variables to see what gave me the result I wanted. Now, I don't understand why it is giving me the right result:
include
include
int main(void)
{
int height;
do
{
height = get_int("Enter the height of the pyramid.\n");
}
while(height < 1 || height > 8);
for (int i = 0; i < height; i++)
{
for (int space = (height - 1); space > i; space--)
{
printf("x");
}
for (int hash = 0; hash <= i ; hash++)
{
printf("#");
}
printf("\n");
} }
For the spaces, why would the space be depended on 'i'?
From my understanding, 'i' will start at 0 (from the outer for loop) and increase by 1 until it is the same number as the height (so if the height entered was 3, 'i' will stop at 2 (0, 1, 2).
So if my space-- is dependent on 'i', in an example where the user enters a value of '4', based on my code above:
First row will initiate with 3 spaces (4-1), check if its more than 0 (the initial value of 'i'), and decrease the space by 1 if True.
Second row will now start with 2 spaces, check if its more than 1 (the current value of 'i' as i++ from the first loop), and decrease by 1 again if True
Third row will now start with 1 space, check if its more than 2 (the current value of 'i' as i++ from the second loop), this will equate to False (as 1 space is less than the 'i' value of 2), so the loop will stop meaning the 3rd row never gets printed.
So from my user who entered a height of 4, the program should only return 2 rows? I don't understand why this program is working fine.
Also why would the hash start from 0? Surely the first row needs to start with 1 hash?