Pascal's triangle: Difference between revisions
Content added Content deleted
(added c++) |
|||
Line 130: | Line 130: | ||
Note: Unlike most Basic implementations, FreeBASIC requires that all variables have been declared before use |
Note: Unlike most Basic implementations, FreeBASIC requires that all variables have been declared before use |
||
(but this can be changed with compiler options). |
(but this can be changed with compiler options). |
||
=={{header|C++}}== |
|||
<cpp>#include <iostream> |
|||
void genPyrN(int rows) { |
|||
if (rows < 0) return; |
|||
// save the last row here |
|||
int *last = new int[1]; |
|||
last[0] = 1; |
|||
std::cout << last[0] << std::endl; |
|||
for (int i = 1; i <= rows; i++) { |
|||
// work on the next row |
|||
int *thisRow = new int[i+1]; |
|||
thisRow[0] = last[0]; // beginning of row |
|||
for (int j = 1; j < i; j++) // middle of row |
|||
//sum from the last row |
|||
thisRow[j] = last[j-1] + last[j]; |
|||
thisRow[i] = last[i-1]; // end of row |
|||
delete [] last; |
|||
last = thisRow; |
|||
for (int j = 0; j <= i; j++) |
|||
std::cout << thisRow[j] << " "; |
|||
std::cout << std::endl; |
|||
} |
|||
delete [] last; |
|||
}</cpp> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |