Welcome to the section challenge.
This has been a long section, and there was a lot to learn.
Pointers can be tricky and intimidating.
But here's a real nice challenge that'll help you understand pointers
in dynamic memory allocation.
So I've given you a bit of a start.
I've written part of the main for you, and you just
have to write two functions.
You have to write a print function that should be pretty easy.
And then you have to write an apply all function that I'll talk
about in a second, and we'll walk right through this real quick.
Okay, so let's go back up to the top of the challenge here.
And what I'd like you to do is write those two functions, but the first
one let's talk about the first one.
Write a c++ function named apply all.
Now this function expects two arrays of integers and their sizes, and it
dynamically allocates a new array of integers whose size is the size of
one times the size of the other then.
What you want to do is you want to loop through the second array
and multiply each element across each element of the other array.
It sounds complicated, it's not.
Let me walk you through exactly what I'm talking about.
So here's the function so it expects two arrays, array1 and array2.
And it expects the sizes for those two arrays.
You can see right here array1, array2 5 and 3.
Okay, So let's say that this is array1.
It's one 1 2 3 4 and 5.
This is array1.
And then we've got array2, which contains 10 20 and 30.
Okay, So we're passing these two pointers basically, right, the
array names into that function.
And we're passing along 5, which is how many elements are in
this array, and 3, which is how many elements in this array.
Now that function is going to dynamically allocate an array
that's 15 big, 5 times 3, and those numbers could vary.
So in this case, what we want to do is we want to take each
element in array2 and multiply it across all the elements of array1.
So 10 times 1 is 10.
10 times 2 is 20.
10 times 3 is 30.
We got the 40, and we have a 50.
Okay, then what we want to do is we want to go through here.
And now go to the next array element in array2.
So that's 20.
So 20 times 1 is 20.
20 times 2 is 40.
20 times 3 is 60.
20 times 4 is 80.
And finally, 20 times 5 is 100.
Okay, then the last thing is go through that last number.
So now we're at the 30, and we're going to go 30 times 1, which is 30.
You get the idea 60 90 120 150.
So this here is the newly created array that you're going to
create dynamically on the heap.
We want to return the address of this array, which is the address of this
guy right here from the function.
That function returns the address, which we store right here in
that pointer variable results then we pass results to the print
function with a 15, and this is what we get from this example, let
me clear the screen real quick.
So here's array1.
I call the print function.
I pass array1 into it, and it's going to display that.
Then I output array2, and then I call the print function.
I pass in array2 with a 3.
I get that.
I call this function.
It dynamically creates that array.
It does the multiplication.
It stores all the values into the the dynamically allocated array,
and it returns the address of that array, and we store it in results.
Then we pass results in 15, and that's what we get.
Okay, so what you need to do again one more time.
You need to write the apply all function and the print function.
Now the apply all function expects array1 and its
size, array2 and its size.
And you could decide what the types are going to be.
The print function expects an array and a size.
Now obviously, this guy returns a pointer to an integer.
This guy doesn't return anything.
So that's roughly your your function prototypes here.
You just have to decide what these types here are going to be.
Okay, that's it.
Test your code, and have fun.
If you need to use the debugger, go for it.
Meet me on the other side of the challenge, and
I'll show you my solution.