0

This is what my program outputs:

Please give me an height for your tower between 0 and 23.
4
....###
...##
..#
.
jharvard@appliance (~/Dropbox/pset1): 

The meat of my code for running this is below: i = height; r = rows; s = spaces; h = hashes:

for(int r = 0; r < i; r++) 

                    {
                        for (int s = i-1; s >= r; s--)
                        {  
                            printf("."); 
                        }
                        for (int h = r+2; h <= i; h++)  
                        {
                            printf("#");
                        } 
                         printf("\n");
                    } 

Line by line explained below.

for(int r = 0; r < i; r++) 
                    {
                        for (int s = i-1; s >= r; s--)

//here I want my spaces to start at my height (i) - 1. //example: integer given height (i) of 4 should start my spaces to be 3 (4-1). As long as my spaces (s) are greater than or equal to //my rows, then i want to subtract one.

{  
                        printf(".");

// i had it print "." so i could visually see the spaces. Will get rid of the "." //when my code is correct.

}
                    for (int h = r+2; h <= i; h++)

//here I wanted to start my Hashes (h) to be equal to my rows+2. //if i=4 again, then my hashes should start at 2 since my rows start at 0. (0+2), next time round (1+2), then (2+2)etc. //my hashes would stop once my hashes = my height. This would allow my hashes to run the correct amount of time. //1st: (0+2), 2nd (1+2), 3rd run (2+2) which means it's still true of h <= i (4) so run one more time (3+2). //The last time would be (3+2) or 5 hashes at the bottom.

{
                        printf("#");
                    } 
                     printf("\n");

//After my spaces and hashes run once, give me a new line before running again.

}

When I write my code in pseudo, it makes sense to me and should run. I'm not sure, since I'm doing this by myself, if I'm just missing something over and over again and need a second pair of eyes, or what's wrong. Can someone please help me with my logic?

2 things I don't understand:

1) Why are my spaces 1 more than what I asked? Example, I set my spaces to = i - 1. Instead, as you'll see in the first row, it is spacing my "i" and not subtracting 1 initially like I had asked.

2) Similar to my spaces, my hashes should start at 2 and add one each time ran. Instead it's subtracting 1 each time through. No clue as to why.

Please help and thank you for your time! Sorry this is so long. I have spent hours reading other questions and answers and thought I had finally figured it out this morning. Now that this doesn't run properly, I'm baffled as to what to change or where to begin.

1 Answer 1

1

Let's see if I can point you in the right direction here.

For spaces, for (int s = i-1; s >= r; s--) is giving you one too many spaces every time because of the test condition. Let's check out a height of 1. You want 0 spaces, but look what happens. i=1, so s= 1-1 = 0. On the first iteration, s >= i-1 tests as 0 >= 0 which evaluates as true. So, it will print a space when you don't want one. It also means that it will execute once too often for any height greater than 0. So what could you change in the test to fix this?

For hashes, it's a different issue. for (int h = r+2; h <= i; h++) Your logic seems to be about how many hashes should be on each row of the pyramid, but you should be thinking about how many hashes to print on the current row only. You are starting at 2 and working up to the number of rows, which prints the difference between those numbers on each line. It will print one less hash on each subsequent row. On paper, try working out what happens as you cycle through the loops. Look at what each value is with each cycle through the loop, like I did earlier.

To fix this, use your starting point as your limit. r+2 is actually the number of of #s that you want to print on each line, starting with r=0.

for(int h = 0; h < r+2; r++ )

This will give you ## for the first row, ### for the second, and so on.

This should get you started. If this answers your question, please mark your question as answered. Let's keep up on forum maintenance. ;-)

2
  • Ohhhh....that makes sense. I was mistakingly using initialization of the starting point and thinking that was the number of spaces or hashes I would get instead of thinking about the number of spaces or hashes I want on each row and thinking about how often I want the for loop to actually run. I can't believe I was thinking about it that way. Definitely needed another pair of eyes on this one. I had tunnel vision and couldn't get past the mistakes I was making. Thank you very much for the detailed answer. Really appreciate it!!!!
    – Jzag
    May 10, 2015 at 3:25
  • I GOT IT!!!!!!!!!!! Thank you so much! I have spent so many hours trying to figure this one out because of those two mistakes I couldn't look past. That's awesome! Thanks again!
    – Jzag
    May 10, 2015 at 3:44

You must log in to answer this question.

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