0

This is as far as I've gotten with Itsa Mario. I've got the first part of the problem down just fine, but I'm extremely tripped up by the second part. I'm very curious because I know that the spaces before the hashes should be (height - 1), but when I put that instead of ++s I get an endless loop of nothing until I close out the terminal. I don't understand why that is, so I put ++s in there to keep that from happening. If someone could explain how I'm getting the endless loop when doing so I would appreciate that.

  #
#
#
#
#
#

This is what comes out currently. I've got x number of hashes high now, but I cannot figure out how to program the certain number of spaces and the certain number of hashes. I believe I can start i at 1 and then hashes would equal i + 1 in a loop? I'm so confused at this point. I've been on this second half of Mario for five days now after figuring out the water problem and the first half on my own on the first night of working this pset. This second half of the problem feels like such a jump but I feel as though once I figure out how to do it I'll wonder how it ever took me 10+ hours to figure out!

I'm not looking for an answer (which is why I warily read many other Mario answers but not all of them because I don't want to know how to code it), I am just extremely perplexed at this point. I've spent around two hours a day on this since Saturday night and I'm struggling a lot. It's not because I'm stupid, right? This is my first experience with programming and I'm finding it very challenging. I enjoy the challenge but at this point I'm now running blindly into walls with no idea of where to go from here. I'm randomly plugging in integers and I don't like coding that way because I'm not learning, it's just an ugly version of guess and check.

I've read through this (Stuck on pyramid for mario.c) answer a few times but I'm still lost on how to print the spaces and hashes. That doesn't mean I'm not cut out for programming, does it? I'm sorry if this is extremely similar to everyone else's Mario question but hopefully someone will be able to guide me in the right direction without answering the question for me.

CODE IS BELOW.
Spaced out so I don't spoil anyone else's pset experience.








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

int main(void)

{ 
    int height;

    //Select the height of Mario's pyramid.
    do
    {
        printf("Choose a number up to 23:\n");
        scanf("%i", &height);
    }

    while(height < 0 || height > 23);

    //Pyramid loop.
    char hash = '#';
    int h = 0;
    char space = ' ';
    int s = 0;
    int i = 0;

    for(i = 1; i <= height; ++i)
    {
        for(s = height - 1; s <= height; ++s)
        {    
            printf("%c", space);
        }
        for(h = i + 1; h <= height; h = ++i)
        {
            printf("%c", hash);
            printf("\n");
        }
    }

    return 0;
}

2 Answers 2

1

You are pretty close! Think about it: You must print a different number of “#”, a different number of “spaces” into each different line. Then, we have 3 variables. Try use a variable “counter” to specify, within the line you are working in, how many “#” and “spaces” I should use. - First loop represent each line - Nested loop 1 – print out “spaces”. You will print (h - 1) spaces, since you set “h” as number of lines and starts “0”; - Nested loop 2 – print out “#”. You will print (h + 1). Good luck!

0

First off I want to point out that you can initialize variables when you first declare them. Instead of int i; followed by i = 0;, you can do int i = 0;.

Your for loops are written like this for(i = 0; i < height; ++i) (where i is declared earlier in the program). Your code will overall be more succinct and easier to read if you write them like: for (int i = 0; i < height; i++) -- where a space follows "for" and precedes the condition, the loop-variable is declared inside the loop, and the final statement says i++ because postfix increment is just the common way to do it and this is happens when your code is run:

for (int i = 0; i < x; ++i)
{
    // From this point on "i" == 1, not 0.
    // The increment happened before the loop ran for the first time, not after.
}

You don't have to printf like this printf("%c", hash/space), printf(" ") or printf("#") is a much better way to do this, in fact you even save yourself two whole lines of code!

Also, since you're struggling so much with even the first pset I highly recommend using CS50's own GetInt(). So then the do-while loop would say int height = GetInt();.

Hopefully a better written program will help you see what's going on and why. Ask yourself not why your code isn't working, rather ask why it is not printing more than one hash per line.

1
  • Thank you, I've cleaned up my code and that's helped things to look nicer. It's not exactly making me see what's going on but it's helping make my guess and check process cleaner. Sep 9, 2016 at 23:49

You must log in to answer this question.

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