0

So I was just trying out this question, just wanted some help in the code, I am not able to identify the problem here, so this is the question

Write a program in Java, to input a 2 dimensional array to generate a Pascal's triangle, it should specifically look like this

1
11
121
1331
14641

So here is my java program for this question:

public static void main(int row) {
    int Arr[] = new int[row][row];
    
    int i = 0, j = 0;
    
    for (i = 0; i <= row; i++) {
        for (j = 0; j <= row; j++) {
            Arr[j] = Arr[i] + 1;
        }
    }
}

I don't really know where to start, I tried using the logic that if taken for a square matrix then the upper two will sum up to the lower one, but I don't really think my logic is valid here, because I did many mistakes in the logic, I just don't know how to make this one, I have no problem in the printing part, so I omitted that from the program.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • For future reference, questions like this should show example input (if any), expected output, and actual output. So, next time, consider it might make it easier for us if you were to include both the printing part, and the output it produced. – Old Dog Programmer Aug 28 '23 at 18:52

2 Answers2

1

Solution

public class PascalTriangle {
    public static void main(String[] args) {
        int numRows = 5; // Number of rows for Pascal's triangle
        int[][] pascalTriangle = new int[numRows][];

        // Allocate memory for each row's inner array
        for (int i = 0; i < numRows; i++) {
            pascalTriangle[i] = new int[i + 1];
        }

        // Fill the triangle using the rule: triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    pascalTriangle[i][j] = 1; // The first and last element in each row is 1
                } else {
                    pascalTriangle[i][j] = pascalTriangle[i - 1][j - 1] + pascalTriangle[i - 1][j];
                }
            }
        }

        // Print Pascal's Triangle
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(pascalTriangle[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Explanation:

  1. We first create a 2-dimensional array pascalTriangle to store the triangle values.
  2. We initialize the first column of the triangle with 1's, as each row's first and last element is 1.
  3. We then use a nested loop to calculate the values for the remaining elements of the triangle based on the rule: triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j].

Output:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
Diego Borba
  • 1,282
  • 8
  • 22
0

If you look up Pascal's triangle on Wikipedia it says. In Pascal's triangle, each number is the sum of the two numbers directly above it. and shows an animated gif as a demonstration.

Here is a solution based on that idea.

int nRows = 5;
int[][] pascal = new int[nRows][];

for (int n = 0; n < nRows; n++) {
    pascal(pascal, n);
}
for (int[] rows : pascal) {
    System.out.println(Arrays.toString(rows));
}

prints

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]

This method takes the pascal triangle being constructed and the next row to add.

  • if the row is the first (0th) row, then add that row to the 0th index of the triangle and return.
  • otherwise, create a currentRow to construct, and obtain the previous row from pascal (Note: multi-dimensional arrays are arrays of arrays and working with 1D arrays is, imo, cleaner).
  • set the first item of the currentRow to 1. This will also be done to the last item of same row. Note that pascal triangle rows are numerically palindromic.
  • now just start setting the ith item in currentRow to the sum of the two directly above it.
  • And when finished set the last element to 1 and assign the currentRow to pascal[row]

public static void pascal(int[][] pascal, int row) {
    if (row == 0) {
        pascal[0] = new int[]{1};
    } else {
        int[] previousRow = pascal[row - 1];
        int[] currentRow = new int[previousRow.length + 1];
        currentRow[0] = 1;
        
        for (int i = 1; i < currentRow.length - 1; i++) {
            currentRow[i] = previousRow[i - 1] + previousRow[i];
        }
        
        currentRow[currentRow.length - 1] = 1;
        pascal[row] = currentRow;
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39