C++ Pascal's triangle
c++, pascals-triangle, recursion
Solution
Your algorithm contains a couple of unnecessary predicates for the base cases. It can be stated more simply as follows:
int pascal(int row, int col) {
if (col == 0 || col == row) {
return 1;
} else {
return pascal(row - 1, col - 1) + pascal(row - 1, col);
}
}
This of course assumes that you're guaranteeing that the arguments passed to the function are non-negative integers; you can always include an assertion if you can't impose such a guarantee from outside the function.
Problem
I'm looking for an explanation for how the recursive version of pascal's triangle works The following is the recursive return line for pascal's triangle. ``` int get_pascal(const int row_no,const int col_no) { if (row_no == 0) { return 1; } else if (row_no == 1) { return 1; } else if (col_no == 0) { return 1; } else if (col_no == row_no) { return 1; } else { return(get_pascal(row_no-1,col_no-1)+get_pascal(row_no-1,col_no)); } } ``` I get how the algorithm works What I wonder is how the recursion does the work.