The below is what I have come up so far and the result is very underwhelming. It just doesn't work out as I expected. The first part works well but I am struggling with the second one. Any help would be appreciated:

[code removed per academic honesty policy]

the result:

enter image description here

  • If it makes you feel any better I am here also. What I do know is that you need to code in a mathematical function to carry out the pattern. One function would be to to calculate the amt of blanks space --height-hashes the other is to increment the hashes. So with that I would appreciate any help you could give me. – Julie Binko Feb 8 '15 at 18:30
  • The output you post doesn't correspond with the code. You don't need the variable s or getString(). Maybe you should rewatch zamyla's video – wallek876 Feb 9 '15 at 7:10
  • variable 's' is irrelevant to 'for' loop. I included it just because a part of the condition of this assignment is to let users to select a string as a height. – Eglė Šeduikytė Feb 10 '15 at 18:56

look at this line:

for (hash = height + 1; hash <= height + 1; hash++)

what happens if height = 3?

  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – wallek876 Feb 9 '15 at 6:55
  • yes, it does. I don't think it makes sense to correct the code, rather point the person to the problem. His for loop is wrong and that's why it fails. – gitterb Feb 9 '15 at 10:54
  • Thank you, @gitterb. It was helpful as now I understand that line doesn't make any sense. However I still struggle to make it right: for (hash = 1; hash <= height +1; hash++) - I just don't get why this is not correct. I think this line should print 2#, then 3#, 4# and stop but it doesn't :/ – Eglė Šeduikytė Feb 10 '15 at 20:29

There are two major issues and a couple of other minor bugs that I'll let you find on your own later.

First, let's deal with the spaces.

for (space = height - line; space == 0; space--)

This line controls the printing of spaces. (You might want to change the space to something printable while you debug so you can count them.) Your thinking is that you want the for loop to stop when space gets to 0, or when the space == 0 condition becomes true. The problem is that the for loop only executes the code while the condition is true and stops when it becomes false. Because of this, the for loop that prints spaces will only execute when height - line equals 0. that only occurs at the end of the process. If, on the other hand, you used space > 0, it will do much better.

Now, the printing of the hashes takes a little different remedy, even though it's a similar issue.

 for (hash = height + 1; hash <= height + 1; hash++)

There are two issues here. First of all, remember that the test condition must be true for the loop to run. If hash = height + 1 to start, it will always be true the first time through, but with one incrementation of hash, it will be larger, so the test will fail.

The second problem is that you are basing your test on the full height of the pyramid. It needs to be stepped up as you print each row, so it needs to be based on the row number, not the height.

There's an old saying: KISS -- keep it simple, stupid! Not meant as an insult or a judgment, this is something I tell myself quite often and it saves me a lot of grief!! So, with that in mind, let's look at the hashes.

The first row always prints two hashes, ##. So, start with hash = 1. But what should the test condition be? Since you want two hashes in the first line, how about line + 1? It makes it simple and gets you there fast. Here's a better test:

for (hash = 1; hash <= line + 1; hash++)

That should get you the pyramid. As was mentioned elsewhere, just delete the string input code. It's not needed. GetInt() will deal with any text inputs.

There's one more bug, but I'm sure you'll find it very quickly. ;-)

This is an older question, so you've probably finished the pset. However, if this or any other answer has satisfied your question, please mark one as the answer to this question so that it will close. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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