0

Ok so after a lot of effort I decided how I wanted to get the spaces done.

Now this seems like a tiny problem to me: the multiplication sign doesn't work, the terminal ... like doesn't recognise it.

Heres what Im trying to do:

for(int r = 0; r < height - 1; r++){

    for(int s = height -1; s > 0; s--){

        printf(" " * s);

    }

    printf("\n");

}

don't worry about the user input part;

Neil

1

I spent many hours on this and finally solved it tonight... just haven't ran it through the CS50 checker yet.

BLUF:(Bottom line up front:) your heading in wrong direction thinking of multiplication.

You will be building a ("FOR LOOP") with two inner ("FOR LOOPS").

So many people say and advise to use pencil paper and eraser (will be erasing A LOT!) but it is one of those statements people blow off. I did anyways, I drew the picture of what I wanted to get a formula, so I had an idea of where to start but I didn't take the advice as literal as I should.

The outer loop will be for your "rows" or w/e you call them. Not giving the answer there because you will find that much on different pages. I tested my row loop by making it print hello times what my user input height would be. I played with 5 just because its easier to look at. Once it printed my 5 hello's (on different lines) I deleted the print hello part and went to what I really wanted it to do... print spaces (I used dashes until I figured it out so I could count them).

This is where pen and paper comes in handy... so you have a height of say 5... after you draw out what you want your pyramid to look like, count the dashes used to get to the hashes... Now, from there figure out variables that will make the computer draw a dash for you... I did this by saying ok, I started row "0" or "1" w/e you choose, now what "number" is <= "this number"... so think how a computer will (flow charts help) is "x" less than or equal to "y"... no, so print a "-", the "++" at the end means after it prints once, it will add a number to your "x" value... is that new "x" <= "y"... yes do it again, hence a loop.. once the value of "x" is > "y"... no, move to next loop of hashes... and repeat same logic... off to next loop...once "a" is > "b" what do you want to happen... you need a new line, but where should this be placed? You want it inside your original "Row Loop" but not inside the two inner loops.

Computer prints new line... starts "Row Loop" over, goes inside two inner loops and repeats process... NOW!, remember the row constant has just went up 1 number so adjust your two inner variables (X and A) to reflect to help you keep the rotation going... once all data meets its criteria, it will rinse and repeat until your rows = user input.

hope this is just babble... good luck... I fought over 24 hours (separate sittings) at my computer and on paper working on this. You will feel very accomplished once you have it, definitely if you are like me with ZERO programming background.

1
  • 1
    Thanks, this really helped.. I think i got it.. – Nilabha Chatterjee Jul 5 '15 at 6:00
0

You can't use the multiplication operation like that, it just doesn't work that way. If you want to do something repeatedly and to vary it from one execution to another, you need to use something like a for loop. For example:

for(int k=0; k < 3; k++)
{
    printf("This is row %i.\n", k);
}

This will print the following:

This is row 0.
This is row 1.
This is row 2.

You should go back and review the class material for a better understanding of the topic.

You must log in to answer this question.

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