Need help with some code (Number Pyramid)

edited April 2016 in How To...

I need help making a program that makes a pattern like this with 'for loops':

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Answers

  • Sorry that didn't come out correctly, but it is supposed to look like this:

    Screen Shot 2016-04-20 at 5.53.55 PM

  • Start with something simple and work your way up to it! Given a number X, can you print out a SQUARE of characters that is X long by X wide?

    For example:

    4:
    XXXX
    XXXX
    XXXX
    XXXX
    
    6:
    XXXXXX
    XXXXXX
    XXXXXX
    XXXXXX
    XXXXXX
    XXXXXX
    
    2:
    XX
    XX
    
  • Now instead of X's, can you print out NUMBERS?

    For example:

    4:
    1234
    1234
    1234
    1234
    
    
    6:
    123456
    123456
    123456
    123456
    123456
    123456
    
    2:
    12
    12
    
  • edited April 2016 Answer ✓
    /**
     * Digit Pyramid (v1.01)
     * GoToLoop (2016-Apr-20)
     * forum.processing.org/two/discussion/16145/need-help-with-some-code
     */
    
    final int ROWS = 9;
    
    for (int n = 1, r = 1; r <= ROWS; ++r, n = 1) {
      String s = "";
      while (n < r)  s += n++;
      s = nf(int(s), ROWS - 1).replace('0', ' ');
      while (n > 0)  s += n--;
      println(s);
    }
    
    exit();
    
  • edited April 2016

    How about with X's AND numbers, in a triangle?

    Like this:

    4:
    XXX1
    XX34
    X234
    1234
    
    
    6:
    XXXXX6
    XXXX56
    XXX456
    XX3456
    X23456
    123456
    
    2:
    X2
    12
    

    Hint: How many X's are you printing on the first line? How many X's less do you print on the next line, and the ones after that?

  • Thanks so much!

  • Homework

    Don't use gotoloops code, it is too dense for you, rewrite it so it matches your skill level

  • 100+plus for what Chirsir said. No homework should asked here.

Sign In or Register to comment.