Recursion worksheet #1 - Homework #8
1) Given a vector of integers, you want to write a
recursive function that will return the sum of the values.
the function prototype should look like this:
int sumfunction(apvector<int> &vector, int a);
You can also assume that a precondition of this is that the function is
called with a being 0.
You can think of it as translating the following code:
int sum;
for (int a = 0; a < vector.length(); a++) {
sum += vector[a];
}
2) Pascal's triangle is a mathematical pattern that
starts as follows:
1
1 1
1 2 1
1 3 3 1
1 4 6
4 1
1 5 10
10 5 1
and so on forever
One way to think of this is that the item in the upper left is always
1, and everything else can be seen as the sum of the value directly
above and directly above and to the left. For instance, the
number at position x=3, y=4 is 6. (3+3=6)
Now of course you need to write a recursive function to calculate any
item in the triangle.
The function prototype should look like this:
int calctriangle(int x, int y);
Tip! This is a much easier function to write if you implement a
check on the code to be sure that ONLY valid xs and ys are input.
Remember, neither x nor y can be negative, and x can't ever be bigger
than y. If you get an invalid number, just return 0 as the
value. (that makes handling the edges of the triangle much easier)
3) Write a function that will translate an apstring
to upper-case via recursion.
The function prototype should look like this:
void makeupper(apstring &a, int currentpos);
Again, you can assume the precondition is that currentpos starts at 0