Skip to main content

Section 5.3 Calculating the Sum of a Vector of Numbers

We will begin our investigation with a simple problem that you already know how to solve without using recursion. Suppose that you want to calculate the sum of a vector of numbers such as: \([1, 3, 5, 7, 9]\text{.}\) An iterative function that computes the sum is shown in Task 5.3.1.a. The function uses an accumulator variable (theSum) to compute a running total of all the numbers in the vector by starting with \(0\) and adding each number in the vector.

Exploration 5.3.1. Iterative Sum of Vector.

(a) C++ Implementation.

(b) Python Implementation.

Pretend for a minute that you do not have while loops or for loops. How would you compute the sum of a vector of numbers? If you were a mathematician you might start by recalling that addition is a function that is defined for two parameters, a pair of numbers. To redefine the problem from adding a vector to adding pairs of numbers, we could rewrite the vector as a fully parenthesized expression. Such an expression looks like this:
\begin{equation*} ((((1 + 3) + 5) + 7) + 9) \end{equation*}
We can also parenthesize the expression the other way around,
\begin{equation*} (1 + (3 + (5 + (7 + 9)))) \end{equation*}
Notice that the innermost set of parentheses, \((7 + 9)\text{,}\) is a problem that we can solve without a loop or any special constructs. In fact, we can use the following sequence of simplifications to compute a final sum.
\begin{equation*} \begin{split} total & = (1 + (3 + (5 + (7 + 9)))) \\ total & = (1 + (3 + (5 + 16))) \\ total & = (1 + (3 + 21)) \\ total & = (1 + 24) \\ total & = 25 \end{split} \end{equation*}
How can we take this idea and turn it into a C++ program? First, let’s restate the sum problem in terms of C++ arrays. We might say the sum of the vector numVect is the sum of the first element of the vector (numVect[0]), and the sum of the numbers in the rest of the array (numVect.erase(numVect.begin()+0)).
In this equation \(first(numVect)\) returns the first element of the array and \(rest(numVect)\) returns an array of everything but the first element. This is easily expressed in C++ as shown in Task 5.3.2.a.

Exploration 5.3.2. Recursive Sum of Vector.

(a) C++ Implementation.

(b) Python Implementation.

There are a few key ideas while using vector to look at. First, on line 6 we are checking to see if the vector is one element long. This check is crucial and is our escape clause from the function. The sum of a vector of length 1 is trivial; it is just the number in the vector. Second, on line 11 our function calls itself! This is the reason that we call the vectsum algorithm recursive. A recursive function is a function that calls itself.
Figure 5.3.1 shows the series of\ recursive calls that are needed to sum the vector \([1, 3, 5, 7, 9]\text{.}\) You should think of this series of calls as a series of simplifications. Each time we make a recursive call we are solving a smaller problem, until we reach the point where the problem cannot get any smaller.
Stacked visualization of recursive function calls to sum a list of numbers. The top box shows ’sum(1,3,5,7,9)’ with an arrow pointing to ’1 +’. The next box down shows ’sum(3,5,7,9)’ with an arrow pointing to ’3 +’, followed by boxes for ’sum(5,7,9)’ with ’5 +’, ’sum(7,9)’ with ’7 +’, and the last box ’sum(9)’ with just ’9’. Each box is connected to the next, representing the recursive nature of the calls.
Figure 5.3.1. Series of Recursive Calls Adding a List of Numbers.
When we reach the point where the problem is as simple as it can get, we begin to piece together the solutions of each of the small problems until the initial problem is solved. Figure 5.3.2 shows the additions that are performed as vectsum works its way backward through the series of calls. When vectsum returns from the topmost problem, we have the solution to the whole problem.
Diagram illustrating the resolution of recursive function calls summing a list of numbers. The bottom box shows ’sum(9)’ equaling ’9’, with an arrow pointing left to the next box ’sum(7,9)’ showing ’7 + 9’. Above that, ’sum(5,7,9)’ equals ’5 + 16’, then ’sum(3,5,7,9)’ with ’3 + 21’, and at the top, ’sum(1,3,5,7,9)’ resulting in ’1 + 24’. Each function call resolves to a value that is used in the computation of the previous call, with the final sum at the top being ’25’.
Figure 5.3.2. Series of Recursive Returns from Adding a List of Numbers.
You have attempted of activities on this page.