0

include

include

int main(void) { //USER INPUT

int height;
do
{
    printf("Insert height:");
    height = get_int();
}
while (height < 1 || height > 23);

//Drawing the hashmid!

for (int i = 0; i < height; i++)
{
    for (int s = height - 1; s > i; s--)
    {
        printf("%s", " ");
    }
    for (int h = 2; h < i + 1; h++)
    {
        printf("%s", "#");
    }
    printf("\n");


}

}

1 Answer 1

1

If you haven't watched the walkthrough video for this assignment - definitely do that now - it's very good.

According to the example in the video, the number of hashes starts with two and increases by one each time. You can figure out how many hashes to print on the nth row, by knowing only the row number n. On the first row, you print 2 hashes, on the second row you print 3 hashes, etc... You are always printing one more hash than the row number.

In your code for the hashes:

for (int h = 0; h < height + 1; h++)
{
    printf("%s", "#");
}

height is the same value every time through the loop, so your loop will ALWAYS print the same number of hashes for each row. You should be comparing h to the row number i - to print out one more hash than the row number. And don't forget that i starts at 0 and the rows in the video start at 1.

4
  • First of all. Thank you for your help! I was really lost and maybe got a bit too emotional at times and your comment helped a lot. Now I think I got everything else sorted but I just can't figure out how to print two hashesh from the beginning without making it a full pyramid instead of a half pyramid. Edit: Updated my code
    – Librarian
    Sep 25, 2017 at 14:14
  • I got the solution for the hash from elsewhere but I still don't understand the logic behind it, which is my main aim on this exercise. Thank you for your help anyway! Also getting: ":( handles a height of 0 correctly did not find output "EOF", when checking for correctness. Any idea what this means?
    – Librarian
    Sep 25, 2017 at 14:25
  • @Librarian The project specification states that you must handle heights that are a non-negative integer no greater than 23. Non-negative means that you must handle 0. In your code, when the height is 0, it stays in the input loop. Their sample outputs show what you should do for a height of 0 under the Usage heading - print nothing. Don't worry about "EOF" right now, it means End-Of-File and has to do with their autograder breaking out of your input loop.
    – robert_x44
    Sep 26, 2017 at 18:07
  • Hi "Don't worry about "EOF" right now, it means End-Of-File and has to do with their autograder breaking out of your input loop" Can you please explain how to get rid of this error, im getting the same. "handles a height of 0 correctly did not find EOF"
    – Alpokanto
    Jan 21, 2018 at 17:47

You must log in to answer this question.

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