Now what am I forgetting? What am I leave off? If I run this program, I will have a memory leak. That memory leak will sit there until the program is closed, or until my computer is rebooted. So I'm going to come over here and the think I need to do is I need to clean up my memory.
So here I'm going to get the array size. And then here I get the array size and create the array, and create the dynamic array. And then here I'm going to load the array. And then here I'm going to display the array content.
So you can see one comment per block of code. So one comment per block of code. You just want to explain what you're doing in that block. And then my very last stanza, I need to clean it up. Now, this is an array. So I need to say delete array numbers. And this is a perfect program, so there's no memory leak here.
So I'm going to run this, and it says enter in the array size. So I'm just going to say size five, just so it's not too many. But I can do 5,000. I can do 50,000, no problem at all. So then it says enter in number one, and I'll say 17. Enter in number two, 13. Enter in number three, 24. Seven, 91. So you can see it displays the content of my array, no problem. See how that works?
So this right here crashes it. So the question is how do you really need to do this? And the way that you need to do it is by doing it on the heap by creating a dynamic one. So the variable DP refers to an array of double, assume the integer variable N has been assigned. Declare DP appropriately, allocate an array of N doubles, and assign the resulting pointer to DP.
All right, so DP is going to be assigned a new double array based on the N variable for the size. So the new keyword gives us back a pointer. This is a pointer. So you can see DP points to an array of doubles. And then we just say new double, and then you can put a new variable in here, and this works.
All right, let's see what they're after. You most certainly should be using an asterisk. So they want the full thing. So I'm going to pop over here to the work area. And they want you to actually create it. So they didn't say create it up here, so that kind of threw me. No big deal.
So this is a double pointer DP. So the variable DP should refer to an array of doubles, or they should say create a variable DP that refers to an array of doubles. But anyway, this is what they're looking for. So double pointer DP is going to be assigned the memory position of the double array that is based on the N size. So that's going to be the double array is going to be created over on the heap, C++ is going to give me back a pointer to it because of the new keyword, and I put that pointer into DP, which is a double pointer.
All right, integer variables first and last have been declared and assigned values. Last is greater than or equal to first, awesome. Allocate an integer array they can hold the squares of the numbers between first and last inclusively. So inclusively means it must include first and it must include last. Assign the resulting pointer to the variable squares, which you must declare. All right, then initialize an array to the squares of the numbers from first to last in that order.
So we need to create the array first. And let's see, it's an integer variables. So I'm int pointer. What do they want me to call it? Allocating integer array. Let's see, assign the resulting pointer to the variable squares, which you must declare. So squares is going to be assigned a new integer array. So integer array.
And then how big? You need to be able to hold from the first to the last inclusively. So that means that the first is one and the last is 10, you need to be able to hold one, two, three, four, five, six, seven, eight, nine, 10, like that. So that means for the size, I want to say the last minus the first. Now that's the difference, that's not the number of slots. So I need to add one to that.
So if I say 10 minus 1 is 9. But need 10 slots. So 9 plus 1 makes 10 slots. See how that works? All right, so play around a little bit with that until you really understand it. But you want to go one to 10, so I want to hold one to 10. So if I take the difference, 10 minus 1, that only gives me 9.
But I'm looking at my fingers-- and you all can't see me, but I'm looking at my fingers-- I've got 10 fingers up. So 9 doesn't work, I need to have 10. So if you want to know how many there are, you have to add one. This right here gives you the difference, and if you take the difference and then add one, then that will be how many there are. That will make it inclusive.
So now the next thing I want to do then is you want to initialize the array to the squares of the numbers from the first to the last. So I'm going to create a for loop, and then I'm going to say int I is 0, while I is less than. Now, how many items do I have? Well, I figured out how many items I had already. So I can just use this exact same thing right here.
Last minus the first plus 1. And then I++, and then curly brace, curly brace, and then squares at position I is going to be assigned. And then I'm going to assign it first time's the first. And that's going to give me my square. But I don't want to be the first times the first for every single slot. I want it to be the first and then the number after that, then the number after that, then the number after that, until I get to my last item.
So I need to say the first plus I times the first plus I. So on my first loop around, I is going to be zero, then my next loop around, I is going to be one. Now order of operations, remember order of operations? Multiplication is done before addition and subtraction. So I is going to be multiplied by first here before it's added like that.
Ouch, that's going to mess up my entire calculation. So how do I get around that? I simply put this in parentheses. Because it's parentheses are first in all cases, then exponents, then multiplication and division at the same level left to right, and then addition and subtraction at the same level left to right. So this right here is going to give me my answer. And let's run it and see if it does.
Got a little typo here, or something's going on here. Let's find out why. The array has not been correctly initialized, and the possible locations of error it's saying is right there. Oh, that shouldn't be a times. Whoops. All right, I just demonstrated that I'm a human.
When I'm teaching a class, I make mistakes, and my students help me. And I always appreciate and I always thank them. And I don't try to hide my humanness, because I'm just a guy, I'm just human. And if I can be a software developer, guess what, you can too. I make mistakes I'm human no big deal. You will make mistakes, you're human, no big deal. We get paid for developing software, and part of that is fixing our own errors. And that's OK too. So we're all human, and we can all do this.
All right, so I changed that to plus, hit my Submit, and I'm golden. Now, one thing I wanted to show you with this problem is there's a number of different ways that you can actually do this. For example, I loaded it like this. Actually, you know what? This is not the one I want to show you. I think it's the next one. But let's go look at the next one.
[End of Audio]