-1

I am trying to create Pascal's Triangle, and the line:

return pascalTriangle(row-1, col-1) + pascalTriangle(row-1, col);

in the recursive method that returns int values in the Pascal's triangle, causes

Exception in thread "main" java.lang.StackOverflowError

It only prints one row 1, then throws exception for other rows. What do I need to fix so it doesn't throw any exception, and forms the Pascal's triangle? Here's my code:

import java.util.Scanner;

/**
 * This program takes n input from the user, and forms
 * Pascal's Triangle in n number of rows that the user entered.
 */
public class DD_PascalsTriangle {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter an integer: ");
        // the number of rows created in the Pascal's Triangle
        int n = scanner.nextInt();
        // print Pascal's Triangle
        printTriangle(n);
    }

    /**
     * @param row rows in Pascal's Triangle
     * @param col columns in Pascal's Triangle
     * @return values in the Pascal's Triangle
     */
    private static int pascalTriangle(int row, int col) {
        if (col == 0 || col == row)
            // base case
            return 1;
        else
            // create the values in Pascal's Triangle
            return pascalTriangle(row-1, col-1) + pascalTriangle(row-1, col);
    }

    /**
     * @param n the input from the user, aka the n
     *          number of rows in Pascal's Triangle
     */
    private static void printTriangle(int n) {
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                System.out.println(pascalTriangle(row, col) + " ");
            }
            System.out.println();
        }
    }
}
Community
  • 1
  • 1
dolnuea
  • 33
  • 1
  • 6
  • 3
    You need to use your debugger to see *why* your recursion is not stopping where you think that it should stop. – Hovercraft Full Of Eels Mar 27 '19 at 01:31
  • 1
    The second call from `printTriangle` is `pascalTriangle(0,1)`, and the second recursive call is `pascalTriangle(-1,1)`, then second recursive call is `pascalTriangle(-2,1)`, then `pascalTriangle(-3,1)`, then `pascalTriangle(-4,1)`, then `pascalTriangle(-5,1)`, then `pascalTriangle(-6,1)`, then ..., then **StackOverflowError**. --- I believe your second loop is supposed to be `col<=row`, not `col – Andreas Mar 27 '19 at 01:42

2 Answers2

1

It seems your code is going into an infinite loop as you have a wrong condition for the inner loop. The inner loop is iterating and filling up the stack memory, eventually exceeding the amount allocated by the JVM.

In order to avoid this stack overflow error and and perfect the shape of your Pascal's Triangle you can simply add one extra loop and change the condition for the inner loop.

public static void printTriangle(int n) {
    for (int row = 0; row < n; row++) {
        //Added Spacer loop for getting perfect shape for pascal triangle
        for (int spacer = n; spacer > row; spacer--) {
            System.out.print(" ");
        }
        for (int col = 0; col <= row; col++) {
            System.out.print(pascalTriangle(row, col) + " ");
        }
        System.out.println();
    }
}
Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
Uday Kumar
  • 123
  • 10
0

Change the second loop to iterate upto row and not n.

public static void printTriangle(int n) {
    for (int row = 0; row < n; row++) {
        for (int col = 0; col <= row; col++) {
            System.out.print(pascalTriangle(row, col) + " ");
        }
        System.out.println();
    }
}
Community
  • 1
  • 1
The Roy
  • 2,178
  • 1
  • 17
  • 33