I haven't yet found an answer in these forums to address my problem(s).

My issue has three components:

  1. The pyramid output is upside down. I get the right number of spaces and hashes (first output line has ##, the next ###), but it's reversed. I think my loop code for the hashes is wrong, but cannot figure out WHY.
  2. The check says my code handles a height of 0 correctly, but when I run the program and input 0, nothing happens. I get the jharvard@appliance prompt. What's happening here?
  3. The check says my code DOES NOT handle a height of 2, 23, or 24 correctly. It says "expected output, but not " ###\n##\n". But the output looks exactly like that (albeit upside down).

Here is my code for the loops (Getint works... I think!). I'd love some help on where the errors are because I can't figure it out looking at the code. I drew a table and a pyramid on the paper and I keep coming to the conclusion that my equations are right, but they obviously aren't. Help!

int line, s, p;

for (line = 0; line < h; line++)
    for (s = line; line  < h; line++)
        printf(" ");
    for (p= line; p < s + 2; p++)

1 Answer 1

  1. On the outer loop you are correctly incrementing line by one in every iteration. But on the first inner loop, you initialize s to line and then, instead of incrementing s you are incrementing line again. Your second inner loop correctly initializes p to line and then increments p as it should. Although this should get you going, your equations are correct yet. You need to tinker a little more with them.
  2. The fact that you handle a height of zero correctly, means that you don't print any layers of the pyramid when the height is 0. And indeed you are correctly doing that. The fact that you get another prompt for jharvard@appliance means that your program exited correctly but it didn't print anything (as it should).
  3. The check is giving you these errors because you are not printing the correct pyramid for the given height. As you said yourself, your pyramid is printed upside-down. Correct your for loops and you will get there.

You are really close! Good job! Keep going!

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

  • I got it!!! Thanks for the point on #2, I thought something was wrong. I basically had to go back to algebra class and look at my table again to figure out where the error was in my equation. Problem was the hash loop and I was initializing to the wrong variable. Thank you!!
    – Jo Harr
    Sep 1, 2015 at 20:22
  • Glad I could help! :)
    – ChrisG
    Sep 1, 2015 at 20:41

You must log in to answer this question.

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