Sorry for another annoying mario post. I have been working on this code for awhile and I am having some difficulties. I can get the right number of # and spaces on the last line, and the right number of lines. Put I can't figure out how to get the right number of hashes and on each line. If anyone has any advice that would be great. My code and output is below.

#include <cs50.h>
#include <stdio.h>

int main(void)
    int height;
        height = get_int();
    while (height < 0 || height > 23);

    for (int row = 0; row < height; row++)
    for (int space = 0; space < height - row - 1; space++)  
        printf(" ");
    for (int hash = 0; hash < row + 1; hash++)

**Output, where height is 8**

#########~/workspace/pset1/hello.c/ $ 

I have formatted your code according to the CS50 style guide, take note of that and possibly read through the guide yourself. This is how C code can, and should ideally look. There are other style guides, but when working on CS50 projects, CS50's own guide is the standard.

  1. Your program starts out printing height number of newlines, in this case eight (8).
  2. After that, it will print (height - row) - 1 spaces, in this case it will print (8 - 7) - 1 or zero (0) spaces. NOTE: According to the way I formatted the code, the compiler doesn't "remember" row after it's scope is exited, for example in the space for loop.
  3. Lastly, it will print row + 1 hashes, or 7 + 1 number of hashes.

Alright, so that's what's going wrong, so now how do you fix it? I recommend writing some pseudo-code, mentally following the execution of that code, look for any flaws, if any is found then fix that flaw. Once you find your pseudo-code to be complete, write that code in actual C code, compile and run, then compare the results to the expected results according to your pseudo-code, if anything went wrong then fix the issues. Repeat until success.

For inspiration for the pseudo-code, watch Zamyla's walkthrough (again if you already have).

Keep in mind the code does what you say, not what you expect it to.

Feel free to message me about any concerns. Best of luck!

  • I got to work. Thanks so much for the answer! Nov 16 '16 at 4:21
  • Great job! Please remember to accept the answer to remove this question from the unanswered pool ;)
    – kluvin
    Nov 16 '16 at 6:55

You must log in to answer this question.

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