0

hi i'm having trouble with making the pyramid in regards to the for loop

for (int x = 0; x < n; x++) am i supposed to look at x as starting as first row? if so, is the second for loop be the column? this seems to be the approach that the wk1 section video suggests

if this is the case, how do i multiply the #'s and spaces? do i do this in the for () section or in the printf() section?

i looked at other people's ideas and they seem to be using the x as the amount of spaces or #'s. and the second for loop will also be spaces or #'s (depending on what they used on the first loop)

ps how do i stop a program if its going infinite in the cmd line

2
  • You can stop a program using ctrl+c or ctrl+z Jan 16, 2015 at 5:04
  • I want to see your sudo code for this problem. Untill you right out your sudo code and play around with what you think the algorithm for making a mario pyramid should be. the loop structures aren't going to make sense to you. ctrl + c stops a program in cmd.. i assume you mean in terminal/bash ctrl + c will always stop. check back with the shorts and zamylia for the sudo code.
    – WorWin
    May 14, 2016 at 6:48

3 Answers 3

0

Think of it as a do loop with for loops.

do
{
    int x, y;
    for(x=spaces; x>0; x--)
    {
         (//print spaces)   
    }
    for(y=hashes; y>0; y--)
    {
          (//print hashes)
    }
    //print new line;
    //subtract height;
    //subtract spaces;
    //add hashes;
}
while(height>0);

To add/subtract more hashes/spaces, you have to add that after the for loops have been executed so it will print a different number of hashes/spaces.

4
  • i have no idea how to implement the pattern. im having trouble coding the following idea: row 0 (which is the highest row) has x spaces and row# + 2 hashes where x = height you put in. then the next row has x-1, x-2, etc spaces
    – rp310
    Jan 16, 2015 at 5:40
  • not sure how i should be using the ++ and -- functions with printf
    – rp310
    Jan 16, 2015 at 5:44
  • You shouldn't be using the ++ and -- functions with printf. You can use it in the for loop or after the for loop. Row 0 has x-1 spaces and 2 hashes. Row 1 will have x-2 spaces and 3 hashes. It will always start at 2 hashes. Jan 16, 2015 at 6:21
  • This means that you need to subtract the # of spaces after the for loop in the do-while loop Jan 16, 2015 at 6:22
0

I'm not near a computer now so I can't really help but I can give you a hint its a for loop that I side of it their's more for loops.

0

for (int x = 0; x < n; x++) am i supposed to look at x as starting as first row? if so, is the second for loop be the column?

You've already got the right idea. In each iteration of the first for loop it will run the second for loop a certain number of times (row 0 column 0, row 0 column 1, row 0 column 2, etc) before starting the first for loop again in a new line. That sort of answers your second question on how to 'multiply' the space or hashes. Using the second for loop condition, you can control how many 'columns' of spaces or hashes there are.

Here's how it would look (in a Scratch style code):

 for (repeat for a number of rows)
     for (repeat for a number of columns)
         print ' '
     for (repeat for a number of columns)
         print '#'
     and so on

And finally, if you want to force the program to exit, use Ctrl-C (Mac OS and Windows both).

You must log in to answer this question.

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