A program to generate and display the first 5 lines of a Floyd triangle. (use n=5 and n=11 rows)?

A program to generate and display the first 5 lines of a Floyd triangle. (use n=5 and n=11 rows)?

I got this right now:

void setup() {

  size(800, 600);
  background(0);

  for(int i = 1; i <= 1; i ++) {
    text(i, i * 10 + 100, 100);

  }
}

What I want the sketch to look like:

Floyd

Answers

  • make a for loop around your current for loop

    the outer for loop tells you in which line number you are.

    in the 1st line number, the inner for loop wants to go to 1,

    in the 2nd line number, the inner for loop wants to go to 2,

    etc.

    so the inner for loop takes the current number from the outer for loop as upper bound / max

    have a separate counter for the numbers you are printing

  • edited April 2017

    it's called a nested for loop

        for ... {
            for ... {
            }
        }
    

    after the inner for loop, make a line break, e.g. by saying y+=19;

    when you use y in your text statement

  • So put:

    void setup() {
     
      size(800, 600);
      background(0);
     
    for(int j = 1; j <= 5; j ++) {
      for(int i = 1; i <= 1; i ++) {
        text(i, i * 10 + 100, 100);
     
      }
    }
    }
    
  • How's this:

    int i, j, k;
    
    void setup() {
    
      size(800, 600);
      k = 1;
    loop1: 
    
      for (i = 1; i <= 15; i ++) {
        for ( j= 1; j <= i; j ++) {
          if (k > 15) {
            continue loop1;
          }
          System.out.print(" "+k);
          k ++;
        }
        System.out.println("\n");
      }
    }
    
  • not bad!

    But that's not good.... :

      if (k > 15) {
        continue loop1;
      }
    

    this is sending the sketch in an endless loop between line 7 and 12 as soon as k>15 (and it's starting for loops again and again without ever ending them)

    instead design the for loops so that they just stop and have the right lower and upper bounds (where they start and end at)

    this is your starting point:

    the outer for loop tells you in which line number you are. So the outer for loop is just from line zero to how many lines you want!

    start from here:

    int i, j, k;
    
    void setup() {
    
      size(800, 600);
      k = 1;
    
    
      for (i = 0; i < 15; i++) {
        for (j = 0; j < i; j++) {
          System.out.print(" "+k);
          k++;
        }
        System.out.println("\n");
      }
    }
    
  • for usage of text():

        int numberToDisplay;
    
        void setup() {
    
          size(800, 600);
          numberToDisplay = 1;
    
          textAlign(CENTER);
    
          int x = 19; 
          int y = 19; 
    
          for (int i = 0; i < 15; i++) {
            for (int j = 0; j < i; j++) {
    
              text(numberToDisplay, x, y);
              numberToDisplay++;
    
              //next column
              x+=22;
            }//for
            // next line 
            x  =  19;
            y  += 24;
          }//for
        }//function 
    
  • How about this short way:

    int rows = 5;  
    int k = 1;
    int j; 
    int i;
    
    void setup() {
    
      size(800, 600);
    
      System.out.println("Floyd's triangle:");
    
      for (j = 1; j <= rows; j ++) {
        for (i = 1; i <= j; i ++) {
          print(k + " ");
          k ++;
        }
        println();
      }
    }
    
  • Answer ✓

    Nice!

    Congratulations!!

Sign In or Register to comment.