0

After some manipulation and trial/error, I finally got my code to output the desired spacing for the half pyramid. However, I am having trouble understand one of the relationship between my condition in my second "for" loop (row 23) and the output I am getting in the terminal that I am hoping someone would clarify for me.

Originally I set my condition to "s > 0" which gave me an output of (using 'p' to represent spaces)
ppp
pp
p
I do not know what possessed me to try this, but then I set the condition to "s > 1" which gave me an output of
pp
p

This is the desired result, but when I ran the scenario on paper and I get a different result for the "s > 1"paper trial

Please note: I am aware that in scen. 5, I set the increment to "s++" which should be "s--" like in scen. 4.

What am I missing here? Why doesn't the output match what I have in my paper trial?

I have attached the full code with the desired result below

//Program prompts user for a height and then outputs a half pyramid at that height

#include <stdio.h>
#include <cs50.h>

// n = height, r = rows, s = spaces, b = bricks(hashes)

int main(void)
{
    //Prompt; user must input an integer between 0-23
    int n;
    do
    {
        n = get_int("Half Pyramid Height (0-23): ");
    }
    while (n < 0 || n > 23);

    //Output: printing row, spaces, and bricks
    //print rows
    for (int r = 0; r < n; r++)
    {
        //print spaces
        for (int s = n - r; s > 1; s--)
        {
            printf("-");
        }
        printf("\n");
    }
}
2

If it is for a certain number of iterations, and the counter does not matter, I'd recommend using one of the two forms

for (int counter = 0; counter < repeats; counter++)

or

for (int counter = repeats; counter > 0; counter--)

because both clearly show the number of iterations they do.

In your case, the inner loop

        for (int s = n - r; s > 1; s--)
        {
            printf("-");
        }

is equivalent to

        for (int s = n - r - 1; s > 0; s--)
        {
            printf("-");
        }

and does n - r - 1 iterations. Means n-1 in the first row, n-2 in second, and so on, and 0 at the last iteration of r being n-1.

Not sure how you got to your hand-written result. You start with n=3 and r=0, so the loop should be entered for s=3 and s=2, but s=1 no longer fulfills the condition s>1, so not sure where you found that third -.

3
  • So is that how the computer is interpreting int s = n - r; s > 1 as int s = n - r - 1; s > 0? Which is why I am getting one less in each row? – Pack Pariah Nov 13 '18 at 10:25
  • When I did the hand written scenario, I took the s = n - r (s = 3 - 0 for the first row), tested my condition which passes and printed '-' x 3. Then from my understanding, the s=n-r is no longer relevant and the increment continues to subtract 1 until one of the conditions fail. Am I wrong? – Pack Pariah Nov 13 '18 at 10:29
  • You are right in that n-r no longer matters, but after two iterations you seem to have missed the enter criterion (1>1 is false) and still wrote another dash. – Blauelf Nov 13 '18 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .