How did you do while completing the assignment?
Hopefully you were able to successfully complete it and we're able to troubleshoot any potential issues
you might have had.
In this lecture, we are going to go over my solution to the assignment.
The first thing that I want to do is I want to go ahead and create my function and I know it's going
to take an array.
So we'll just go ahead and give it a vector of eight.
And then I also know that I'm going to want to know the size of the array.
So we'll give it my size as well.
And then if the size is less than one, then we won't really have anything to do because then that will
be the sum of the array.
We also knew from the instructions that we're going to want a temporary array, that we're going to
store the values in, which means that we want it to be mutable.
And since it's going to be an empty array, I'm just going to go ahead and just say Vic Nu, and we
know that we're going to pass this temporary array back into our function.
So now I want to just go ahead and I want to make this a mutable reference.
And now we can begin adding in consecutive elements and storing them into our temp array.
So I'll say four I in zero to size minus one.
We want to say let x equals array of I plus array of I plus one, because now we're getting the consecutive
elements in the array and then we want to push the result of that addition onto our temporary array.
And then once we have done that, we want to be able to print out our array when we are backtracking,
aka unwinding the stack, and then we can simply just say, print out the array.
So now.
We can call our function.
But first we need to create our vector and I'll use the VC macro for quick initialization.
So we'll have one, two, three, four and five.
That way it lines up with what we discussed when we were talking about this problem in the previous
lecture and we will say let's size equal to VEC dot link and now we can call our function and we will
pass it in as immutable reference to our VEC and then we will also pass in size.
So let's double check this real quick to make sure everything lines up.
We're passing in our our vector of integers.
We do our base case so that if our size is less than one, we return.
We create our temporary vector to store our consecutive editions from the array that is passed in as
the parameter, and we push that value onto the temporary array.
And I forgot one very important step.
I'm glad we went over this.
I need to make my recursive call.
So in here we're going to pass in the temporary that we created, and our size is now going to be size
minus one.
So now everything is looking really good.
So let's go ahead and run this.
And we see that we have 48, 20 and 28, eight, 12, 16, three, five, seven, nine, one, two,
three, four, five.
And that is the correct output.
And we did this while executing a recursive, recursive approach.
So hopefully you were able to get to this solution.
It might not be the same as mine, but hopefully your solution used recursion and you were able to get
to the same output as I was.
If you have any questions regarding my solution to this assignment, please feel free to ask them in
the Q&A section.
So hopefully you enjoyed this assignment and I will see you in the next section.