#include #include #include // Problem #1 (tail recursive) void findlargest(const apvector &vector, int position, int &largest) { if (position == vector.length()) return; else if (vector[position] > largest) largest = vector[position]; findlargest(vector, position+1, largest); } // Problem #1 embedded recursive (this will look weird) int findlargest(const apvector &vector, int position) { if (position == vector.length()) return 0; // we assume 0 is the smallest possible value /* ok, this will look very very weird. If we assume that findlargest should return the largest value in the vector starting at a given position, then we can get away with saying we want to return which ever is larger: the item we're looking at now, or the largest remaining value in the apvector, which is what we're saying with the following code */ else if (vector[position] > findlargest(vector, position+1)) return vector[position]; else return findlargest(vector, position+1); } // Problem #2 // this should be a lot easier now int numseries(int position) { if (position > 0 && position <= 3) return 1; else return 2 * numseries(position - 1) + numseries(position - 2) - numseries(position - 3); } // Problem #3 (saving the world from your teacher...) int sl(const apvector &v, const int cutoff, int pos) { if (pos == v.length()) return 0; else if (v[pos] < cutoff) return v[pos]+sl(v,cutoff,pos+1); else return sl(v,cutoff, pos+1); } int sumlower(const apvector &v, const int cutoff) { return sl(v, cutoff, 0); }