0

I am not sure how to get the code working the way I want. I think it should make sense. Should I change my approach on the output section to do...while?

int height, space, hash;    

do
{     
    printf("Let's build! Give me a number between 0 and 23 inclusive.\n");
    height = GetInt();
}   
while (height < 0 || height > 23);         

for (height = 0; height <= 23; height++)
{
    for (space = 0; space == (tower_height - 1); space --) 
    {
        printf(" ");
    }

    for (hash = 2; hash == (tower_height + 1); hash ++)           
    {
        printf("#");
    }

    printf ("\n");
}
2
  • It looks like you have some syntax errors in your second and third for loop declarations. (You could also afford to post much less code - just your Output section - to stay within the bounds of the academic honesty policy.)
    – Air
    Commented Jul 2, 2014 at 23:16
  • You may look at this cs50.stackexchange.com/a/1044/1161
    – kzidane
    Commented Jul 2, 2014 at 23:33

3 Answers 3

4

I found a small but fatal glitch in your code. Have a look on you first loop in output part

for (tower_height = 0; tower_height <= 23; tower_height ++)

A moment before this line in your code, you promted the user to Input height of tower and stored it in the variable tower_height.

And here in for loop you simply initialized it with, tower_height = 0. Hence you simply lost the data you need to process to display that triangle.

Try to declare another variable which start from 0 until it reaches the tower_height

Ex.

for(int i = 0; i < tower_height; i++)
{
    // Second loop
    // code goes here
}

There may be other issues too in the program, but this one was most important to focus at. May be after resolving this issue you get close to your goal.

Wish you all the best

0

Assuming tower_height, space and hash are declared before the loos, as far as I remember, the do..while loop is used to get valid input from the user. As per the pset specification page, you should accept heights between 0 and 23 inclusive.

For your approach, I see no obvious reasons for iterating till tower_height (assuming it's the height you get from the user) is 23.

Also, space == (tower_height - 1) will be evaluated to false if tower_height - 1 is not 0 initially! It's correct that relation between the initial number of spaces and the height is as follows

initial spaces = height - 1

but that doesn't mean that we should check if spaces == (tower_height - 1). Probably we should assign this value to spaces initially before the loops and decrement it accordingly as we move to the next row using a loop to print space white spaces.

for (int i = 0 to i < space, increment i)
{
    print a whitespace
}
decrement space

It's also correct that the initial number of hashes is 2 and the maximum number of hashes is tower_height + 1, but this also doesn't mean that we should check whether hash == (tower_height + i). Rather, we should set the initial value of hash to 2 before the loops and increment it accordingly as we move to the next row using a loop to print hash hashes.

for (int i = 0 to i < hash, increment i)
{
    print a hash
}
increment hash
5
  • I understand conceptually. I'm struggling with the syntax of execution.
    – Mo1
    Commented Jul 3, 2014 at 1:55
  • You may watch the lectures, the walkthroughs, the section and the shorts to learn more about the syntax rules!
    – kzidane
    Commented Jul 3, 2014 at 1:57
  • I did not separate my height from the rows. I was going in the right direction but its like a child learning to talk, you only know a handful of words and concepts. Going and learning more doesn't always help because there is so much to learn. If you can't have empathy for nubs then don't answer questions. for (int row = 0; row <= tower_height; row ++) {for (space = 0; space < (tower_height - row - 1); space ++) for (hash = 0; hash < (row + 2); hash ++)
    – Mo1
    Commented Jul 3, 2014 at 2:41
  • @Mo1 In the world of computer science, there is no one correct way to solve a problem. There is a bunch of correct solutions for every problem. I didn't mean to be rude when I asked you to watch the resources of week 1 again. You said you had a problem with syntax! Apparently you meant the logic, but you didn't use the correct word. You may Google syntax for more info! For your fix to the problem, you may compare between the code posted in the question and the code in your last comment and try to imagine how a programmer other than you can think that this is what you meant to do with your
    – kzidane
    Commented Jul 3, 2014 at 10:13
  • @Mo1 code. I offered a hint to a working solution and a correct answer at the end. And it's up to you to determine how to solve the problem.
    – kzidane
    Commented Jul 3, 2014 at 10:18
0

If this helps someone else what I needed to do was (aside from fix up my syntax and logic) was introduce 4th int as row.

for (int row = 0; row < tower_height; row ++)
{
    for (space = 0; space < (tower_height - row - 1); space++) 
    {
        // here i want to print spaces as long as space is one less than the tower_height
        printf (" ");
    }    

    for (hash = 0; hash < (row + 2); hash ++)           
    {
        // here i want to print #'s as long as hash is one more than the height
        printf ("#");
    }  

    printf ("\n");  

You must log in to answer this question.

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