0

I am trying to draw pascal triangle in java

Here is my code

static void printPascalTriangle(int length){               
    int[][] pascal=new int[length][length];

    for (int i = 0; i < length; i++) {
        pascal[0][i]=1;
        pascal[i][0]=1;
    }  // here length is the dimension of pascal triangle , first loop is for filling up the first row and column with zero 

    for (int row = 1; row < length; row++) {
        for (int column = 1; column < length-row; column++) {
            pascal[row][column]=pascal[row][column-1]+pascal[row-1][column];
        }

    } //  this loop is for filling up the rest of position

    for (int row = 0; row < length; row++) {
        for (int column = 0; column < length-row; column++) {
            pr(pascal[row][column]+"\t");
        }
        prln("\n");

    } // this loop is for printing the array 



}


static void prln(Object anyObject){
    System.out.println(anyObject);
}

static void pr(Object anyObject){
    System.out.print(anyObject);
}

I have used three loops here , How can I solve this problem by using a single loop , or at least two loop , I do not want to use a different loop for printing the array .

7
  • 2
    If this code works, the question is better suited for codereview.stackexchange.com Commented Dec 16, 2013 at 16:22
  • why do you want to avoid the second loop? Commented Dec 16, 2013 at 16:22
  • first try to make it work then try to reduce the code, this'll not draw the pascal triangle. Commented Dec 16, 2013 at 16:23
  • @turbo: Ok I will ask it there Commented Dec 16, 2013 at 17:02
  • @JoeriHendrickx: I want to solve this problem in a short way . Most importantly I want to print a two dimensional array by using a single for loop Commented Dec 16, 2013 at 17:03

1 Answer 1

1

Using this formula we get the algorithm:

// for each row 
for (int row = 0; row < length; row++) {
  // the value of the previous column in this row
  // it doesn't really matter what we initialize this to
  // since we never use the initial value anyway
  int previous = 1;

  // for each column in this row
  // row 0 has 1 column and each subsequent row has one more
  for (int column = 0; column <= row; column++) {
      // the value of the current column in the row
      // the default value is 1, since that is always the value 
      // of the first column in every row
      int current = 1;

      // if the current column isn't the first, use formula
      // from the wikipedia article to compute the current column
      if (column != 0) {

          // we cast 'column' to a double so that the decimal does not get
          // truncated during division
          current = (int) (previous * ((row + 1 - column) / (double) column));
      }

      // print out the current column followed by a space
      System.out.print(current + " ");

      // set the value of the previous column to the current column value
      // this will be used in the next iteration of this for loop
      previous = current;
  }

  // print a new line to start a new row
  System.out.println();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , it seems more efficient and understandable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.