I hope you've enjoyed this section on recursion.
We are now going to have an assignment to really solidify our learning of recursion.
For this assignment we are going to execute the some triangle of array problem.
And what this problem is, is we're going to have a vector of integers and we're going to print a sum
triangle from it.
So in order to get a better understanding of this, let's walk through the problem.
So as I said, one of the first things that we are going to have is a vector of integers.
So we'll call our vector a and we will say inside this vector we have one, two, three, four and five.
And in this case, let's just go ahead and look at the output of what we would expect, a proper function
function to print for us.
So we will have 48 And then on a another line we'll have 20 and 28 and below that we'll have eight,
12, 16 and then we'll have three, five, seven, nine.
And our last line will be one, two, three, four and five.
So you might be wondering how we generate the line preceding above it.
Well, if we look at this, we will see we have one plus two.
Equals to three to plus three is equal.
Five.
Three plus four equals seven and four plus five equals nine.
So three, five, seven, nine.
And that is our next line.
In this line we will do three plus five equals eight.
Five plus seven equals 12.
And then seven plus nine equals 16.
And as you can see, this equals the next line.
And now we will have eight plus 12 equals 20, and 12 plus 16 equals 28.
And then our final line is 20 plus 28 is equal to 48.
So that is how we generate the output.
So if we want to do this recursively, what should our approach be?
Well, obviously, recursion is the key to solving this.
And then at each iteration, you will want to create a new array.
So a temporary array which contains the sum of consecutive elements in the array that was passed in
as the parameter.
And then you're going to want to make a recursive call and pass the newly created array in the previous
step.
And then when we are backtracking, we want to print the array in reverse order.
So what does our steps Recursion?
Obviously, because that is what this assignment is all about.
And then so at each iteration, at each iteration.
Create a temp.
I'm underlining it because this is a very important step.
Array or vector that.
Contains.
This.
Of consecutive.
Due to.
Elements
in the.
Array parameter.
I'm a short net, so what I mean by contains the sum of consecutive elements in the rate parameter and
the first array that was passed in, we passed in one, two, three, four and five.
So the sum of consecutive elements would be one plus two, two plus three, three plus four, four plus
five.
So you can think of that as I plus I plus one and then store this value and then I would go up.
So now it's I plus I plus one.
And we just continue on doing that and then we want to make recursive call.
Recursive.
Call.
And pass in our new array.
Passing the temp.
Ray.
And then print erase when backtracking a.k.a. unwinding the stack.
That way it prints out in reverse order.
So give this a go.
And then in the next lecture, I will go over my solution to this problem.