0

Having a little issue understanding this. in the mario code I'm looking at.

// I understand this repeats the loop "pyramid_height" times. all good here.

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

// here's my question. this will print appropriate number of spaces. Say a pyramid height of 5 -i (say were on the 3rd row so "i" would equal 3 correct?) -1 To me this looks like 5-3-1=1 that would print one space..but I would want to print 2 spaces on the third row.

{ for(int j = 0; j < pyramid_height-i-1; j++) { printf("%s", " ");

FULL CODE BELOW

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

int main(void) 
{

    int pyramid_height = 0;


    do {
        printf("Height:");
        pyramid_height = GetInt();
        if (pyramid_height == 0)
        {
            return 0;
        }
    } while (pyramid_height < 1 || pyramid_height > 23);


    for(int i = 0; i < pyramid_height; i++) 
    {

        for(int j = 0; j < pyramid_height-i-1; j++)
        {
            printf("%s", " ");
        }

        for(int k = 0; k < i+2; k++)
        {
            printf("#");
        }
        printf("\n");
    }   
    return 0;
  • Where did you figure out that you needed to nestle the for loops inside of each other? That was where I was struggling, and couldn't find a good example in the walkthroughs or shorts. – mreliot0811 Mar 30 '17 at 18:59
  • also, did you get the following when you ran the check?~/workspace/pset1/ $ check50 2016.mario.less mario.c :) mario.c exists :) mario.c compiles :) rejects a height of -1 :) handles a height of 0 correctly :( handles a height of 1 correctly \ expected output, but not " #\n" – mreliot0811 Mar 30 '17 at 19:13
  • :( handles a height of 2 correctly \ expected output, but not " #\n ##\n" :( handles a height of 23 correctly \ expected output, but not " #\n ..." :( rejects a height of 24, and then accepts a height of 2 \ expected output, but not " #\n ##\n" :) rejects a non-numeric height of "foo" :) rejects a non-numeric height of "" – mreliot0811 Mar 30 '17 at 19:13
  • @mreliot0811 I got that same error when I ran check50. Did you figure out what the issue was? – Eleanor Meegoda Apr 8 '17 at 19:20
1

You don't need it to be height-i-1 and you can just do height-i

Your hashes can be i+1 rather than i+2

If you do this, I think it will work as planned.

Hope this helps,

Ian

| improve this answer | |
  • Thank you so much. With this help I was able to not only complete the PSET I was quickly able to then finish the "more comfortable" version. – Patrick Fisher Mar 27 '17 at 16:05
  • No problem man! Anytime. – Ianardo Mar 27 '17 at 21:26

You must log in to answer this question.

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