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.