// // example of recursion: // // function to compute sum of elements of array // // main (test) program prompts for array elements // #include // precondition: n >= 0, and array a has at least n elements // postcondition: returns sum of a[0] .. a[n-1] int arraysum(const int n, const int a[]); // ---- main program ---- int main() { const int MAXSIZE = 50; int a[MAXSIZE]; int n; cout << "Enter up to " << MAXSIZE << " integers, ctrl-D to end:\n"; n = 0; while ((n < MAXSIZE) && (cin >> a[n])) n++; if (n == 0) cout << "nothing entered!\n"; else cout << "sum of input integers = " << arraysum(n, a) << endl; return 0 ; } // ---- function definition ---- int arraysum(const int n, const int a[]) { if (n == 0) return 0; else return a[n-1] + arraysum(n-1, a); }