// // example of recursion: // // function to compute element of Pascal's triangle // // main (test) program repeatedly prompts for inputs // #include // precondition: r >= 1, i <= r // postcondition: returns i-th element of r-th row of Pascal's // triangle, where indices of both rows and elements start // with 1 int pascalTE(const int r, const int i); // ---- main program ---- int main() { int r, i; char prompt[] = "Enter row number (positive) and " "element number (positive, <= row number),\n" "or ctrl-D to end:\n"; cout << prompt; while (cin >> r >> i) { if (r <= 0) cout << "Row number must be non-negative!\n"; else if ((i <= 0) || (i > r)) cout << "Element number must be non-negative " << "and <= r!\n"; else cout << "element " << i << " of row " << r << " is " << pascalTE(r, i) << endl; cout << prompt; } return 0 ; } // ---- function definition ---- int pascalTE(const int r, const int i) { if ((i == 1) || (i == r)) return 1; else return pascalTE(r-1, i-1) + pascalTE(r-1, i); }