Questions tagged [pascals-triangle]

Pascal's triangle - is a triangular array of binomial coefficients where the elements of the first row and column are equal to one, and all other elements are the sum of the previous element in the row and column.

Pascal's triangle - is a triangular array of binomial coefficients where the elements of the first row and column are equal to one, and all other elements are the sum of the previous element in the row and column.

T[i][j] = T[i][j-1] + T[i-1][j];

Example:

 1  1  1  1  1  1  1  1  1 
 1  2  3  4  5  6  7  8 
 1  3  6 10 15 21 28 
 1  4 10 20 35 56 
 1  5 15 35 70 
 1  6 21 56 
 1  7 28 
 1  8 
 1 

Iterative method of filling an array:

int n = 9;
// an array of 'n' rows
int[][] arr = new int[n][];
// iterate over the rows of the array
for (int i = 0; i < n; i++) {
    // a row of 'n-i' elements
    arr[i] = new int[n - i];
    // iterate over the elements of the row
    for (int j = 0; j < n - i; j++) {
        if (i == 0 || j == 0) {
            // elements of the first row
            // and column are equal to one
            arr[i][j] = 1;
        } else {
            // all other elements are the sum of the
            // previous element in the row and column
            arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
        }
    }
}
261 questions
-3
votes
3 answers

Drawing a Pascal Triangle with user input (Java)

I'm asking "how many rows" and hoping it prints out the pascal triangle. But I'm getting an error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method nthPascalRow(int) in the type Pascal is not applicable for…
user3019802
  • 65
  • 2
  • 10
-4
votes
1 answer

How do I display a pascal triangle using a 2D array in C++?

Guys how can i display a pascal triangle in for loop using a 2 dimensional array? Here's my code. void print(int a[3][3]) { for (int r = 0; r < 3; r++) { cout << endl; for (int c = 0; c < 3; c++) { // how to display a pascal…
HelpMe
  • 165
  • 3
  • 14
-5
votes
2 answers

error in c++ main file int expression

I am making a new program in c++ i get the current error expected primary-expression before ‘int’ about this line p1::pascals_triangle(int depth); My code is: this is my functions.cpp using namespace std; /** …
Technupe
  • 4,831
  • 14
  • 34
  • 37
-5
votes
1 answer

Pascal's Triangle|Leetcode Wrong Answer

https://leetcode.com/problems/pascals-triangle/ class Solution { public: vector> generate(int numRows) { vector> ret; for (int i=0; i v; if (i==0) { …
-9
votes
4 answers

Pascal Triangle using Java

I have searched in many sites, but I couldn't find the exact output of the following pascal triangle. Can anyone help me out of how to get the following output. 1 1 1 1 2 1 1 3 3 1 1 4 6 …
Kanth
  • 47
  • 3
  • 6
-11
votes
5 answers

How can I calculate the number at a given row and column in Pascal's Triangle?

I'm trying to create a function that, given a row and column, will calculate the value at that position in Pascal's Triangle. Example: val = GetPasVal(3, 2); // returns 2 So here I'm specifying row 3, column 2, which as you can see: 1 …
user2205090
  • 33
  • 1
  • 1
  • 3
1 2 3
17
18