Welcome to the section 12 challenge solution.
I'm in the section 12 workspace in the ChallengeSolution project.
And before I forget, we're going to write those two functions.
But right here on line 108, don't forget to free up the storage.
Okay, I was going to get into the functions and I knew I was probably
going to forget to mention this.
But remember, that function that we're going to write, this function apply
all allocate storage from the heap.
When we're finished with it, we need to release it.
So we want to say delete square brackets results.
All right, perfect.
Now let's go write those functions.
First thing we'll do is we'll write the print function.
The print function is really easy to understand.
And here it is right here.
Okay, so what does print expect.
And here you can see a call to print.
Let me scroll up just a little bit so you can see it right there.
Here you can see a call to print right here on line 97.
You can see what's happening.
I'm calling print, and I'm passing in an array, right, which is the
address of an integer in this case that just happens to be the first
integer in this array right here.
And I'm passing in array1 size, which is a constant defined to 5.
Now this function expects a constant pointer pointing to a
constant, so I can't change the pointer, and I can't change what
it's pointing to in this case.
So any mistakes I make in here, I want caught because I have no
plans I'm changing anything here.
I'm not modifying the pointer or what it points to,
and I'm passing in the size.
So what am I doing here.
I'm just simply displaying that square bracket, and then I'm just
using a real simple for loop.
There's my I.
That's my loop variable.
I'm starting at zero, and I'm going again less than size.
Size in this case is 5.
So I'm going 0 1 2 3 4 and I stop.
And that's exactly how that array is indexed.
At each iteration, I'm just displaying arr sub-I.
Now right here, I'm using pointer, subscript notation.
I could very easily have used pointer offset notation, right,
which becomes arr plus I and de-reference that guy, exactly the
same they both mean exactly the same, so either one you could use.
Okay, so at this point, I'm looping through there.
When i hit 5, I stop, and I've displayed all those values here.
So this will display 1 2 3 4 5 and a square bracket and then a
new line at the end, and that'll be the output you see from here.
Okay, so hopefully, you got this function.
It's a pretty simple function to write.
Most likely, if you had any issues with this, it was probably
right in here, maybe you did an I less than or equal to size and
you had an off by one error and maybe you printed some garbage.
That's a very common error.
If you use the debugger that probably helped you out.
Okay, so let me clear that.
Now let's go and write the main function here, the apply all function.
Now in this case, this is how I've defined this function.
We know it returns a pointer to an integer, right, because it needs
to return the address of that newly allocated space on the heap.
It expects array1, which is a pointer to an integer and the
size of that array and array2, which is a pointer to the other
array and the size of that array.
So that's what we're passing into this function.
I've declared everything is conts const, all these pointers that way I'm
not messing with pointers, and I'm not messing with what they're pointing to.
I've got no business changing those two arrays that are coming in.
I'm just building a third array, which I'm returning.
So here is the third array.
It's called new array.
Right now, it's just a null pointer and it's a pointer to
an integer, right, an address of that first integer in that
new array that I want to create.
And this is me creating it right here, new int, and this is going to
be 15 in this example because I'm passing in a 5 and a 3 right here.
So I'm allocating 15 integers on the heap.
Right now, that's been allocated.
That all contains garbage right now.
And they're indexed from 0 to 14.
Okay, now I want to loop through those arrays that were passed into me.
So that I can start doing some of these calculations.
So let's do that right now.
First of all, I've got this variable called position,
and position starts at 0, and it's going to go from 0 to 14.
That's what I'm going to use to index into my new array here.
But I've got those other two arrays.
I've got arr1, which was 1 2 3 4 and 5 in this example.
And I've got arr2, which was 10 20 and 30.
Okay, so this is where we're at, and obviously we have the sizes that were
passed in the 5 and the 3 as well.
So what do I want to do?
Well, I want to loop through this array as the outer loop
because I want this to loop slow.
This is the one i want to loop fast because I want to stay
at 10 and then go 1 2 3 4 5.
Then I want to go to the 20 and go 1 2 3 4 5 and so forth, right.
So my I looping variable is going to go from 0 to size 2, which is
0, 1 and 2 because that size 2 is 3.
And I want to increment I by 1.
And then I've got an inner loop, so I've got a nested loop here
where my looping variable is J and it's going to go less than size 1.
So we're dealing with 5 here.
And I'm incrementing that by one at each time.
So look what's happening in the body of the inner loop.
Okay, I'm setting the value.
And again, I'm using pointer subscript notation.
I'm setting the value at new array position, right.
Position right now is 0.
We set it right up here.
So I'm setting the value at that new array position to be arr sub-J, right.
At the beginning, this will be 0 times arr sub-I, which is also 0.
So that's these two guys, right, 10 and 1.
So when we first go through here, the second arr2 is the one
that's looping slower, right.
So this guy's going to go 1 2 3 4 5.
This guy is going to go 10 10 10 10 10, he's not changing, right.
So what we're doing is we're multiplying 10 times 1 and
putting it here, right.
Then we're incrementing position.
So now position is 1.
And then we're multiplying 10 times 2, we got our 20.
And we're going all the way down to 50.
And along the way, we're incrementing that position variable.
This is the position variable right here.
Now we're done with the inner loop, so we go back out to the outer loop.
And now instead of a 10 here, we're going to grab the 20, right.
So this guy now refers to 20.
So we're going to do the same thing.
We're going to go 1 2 3 4 5, and then 20 20 20 20 and 20.
And we're multiplying all of these, storing the product right in here.
So we're going to get 20 40 60 80 100.
And along the way, 5 6 7 8 and 9.
We're incrementing that position variable.
Okay, then obviously on the last iteration of the loop, we're
dealing with 1 2 3 4 and 5, and we're multiplying all of these
guys by that 30 right here.
So what we're doing here.
And then of course, this position is being bumped up each time.
So that's what's pointing into the new array, right.
So now here we've got 30 60 90 120 and I ran out of space but
there's a 150 there at the end.
At that point, that array now has been allocated on the
heap and all those values have been stored into the array.
I might clear this now and then all I'm doing here is returning new array.
So it's returning the address of that first element in the array.
Okay, now I'm back here now in my main, and I just returned
that address right here.
So this pointer results is pointing to that array that I
just erased, that big array of 15 integers from 0 to 14 on the heap.
It's got no name.
The only way I get to it is through this results pointer.
Okay, simple as that.
Then I'm using a constant expression here to basically calculate this
constant result size, which, in this case, is going to be 15.
And I'm just passing results and 15 to the print function
and it's going to print all those values that we calculated.
So let's give this guy a run.
And there's the run.
You could see array1 is 1 2 3 4 5.
Array2 is 10 20 30.
And then the resulting array is 15 elements big, and it's a function of
applying that multiplication of one array to another array and putting
the product in the third array.
Okay, simple as that.
If the debugger might have helped you if you used it.
If not, it's just really a matter of -- the only tricky
part here is this piece of code right here the nested loop, and
this is just one way to do this.
You could have done this a lot of other different ways, but this
is a really simple way to do it because you keep that position,
you just keep bumping it up.
And it's going to go from 0 to 14, and it's as simple as that.
All right, so I hope you guys were able to solve the challenge.
If you did it a different way and you think it's more efficient or
pretty clever, let us all know.
That'd be awesome to learn.