MyProgrammingLab 9-8 Part 4 Transcript

Print

All right, so, "The variable ip array has been declared with 20 positions. Each element is a pointer to an integer. Allocate 20 integers, and assign their pointers to the elements." 

I'm going to go ahead and say "4". And "i is 0, while i is less than 20 i plus plus curly brace, curly brace". And then, the ip array has been declared as a 20 position pointer array. So I'm going to say "at position i is going to be assigned a new integer". 

And then, what do you put in here? Well, that's really up to you. You can put any number. They're not telling you specific numbers to put in put in there, so you can put any number that you want. 

In fact, you can actually do this. You can actually say "i". So you can put 0, 1, 2, 3, 4, like that, and then load them. And if I hit Submit, this works. 

I'm going to go back over to the work area. I could have also done this a different way. I could have also did this. Now, this way right here, when I show you, you're going to say, "Oh, that's insane. Are you insane?" 

So here, I'm going to go ahead and put in 0. And then, I'm going to grab this line, and I'm going to do a Control-C. And then I'm going to do a Control-V, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20. 

Oh my gosh, I can't believe I'm actually doing this. Because this is too much, like, work, isn't it? That "for" loop was so much easier. But in a "for" loop, all it does is it takes that first, that i. 

The i initially is a 0, then we put in a 1, then a 2, then a 3, like that. So every time the i loops around to the next iteration, the next loop of the "for" loop, it just increases i by one. That's all that does. 

This is literally the exact same thing, but I'm doing it line by line instead of doing it in a "for" loop. And you can see the "for" loop was so much easier. This right here, it's really a lot like work. This is too much. This is not right. 

But I'm going to show you anyway. And then, 16, 17, imagine if I had to do 50 of these or 100 of these. I simply wouldn't do it. It would be too boring. Doing 20 is bad enough. 

And then over here, I'll put in my 1, my 2, my 3. I'm already getting bored, so I'm going to leave these, the rest of them, as zeros. 

So you can see that I could have gone to each slot of the array, each position of the array, and I could have created that new integer over on the heap and then just loaded it with numbers, like this. And guess what? This works. Do you see that? But this right here is just insanity; this is not the good way to do it. 

But what if I want to do random numbers? There's something else I could have done. If I want to load them up with random numbers, I could do this. I could say "for int i is 0, while i is less than 20 i plus plus, and then curly brace, curly brace". 

And then here, I could create a random number using my rand function. And then I can modulus it against 10, so that would give me a number from 0 to 9. And then I could say plus 1, so that's going to give me, instead of 0 to 9, it's going to be 1 to 10. So this right here would give me a number from 1 to 10, like that. 

And then I could say "the array", the ip array, "at position i is going to be assigned a new integer using that random number", like that. So that would actually put a random number into each slot of my array, which is kind of cool. This is another way I could have done it. So if I hit Submit, you can see that's accepted by the software. 

Let's go over to our next position. And "The verbal Cpp has been declared as an array of 26 pointers." So we have 26 slots. "Allocate 26 character values initialized to the letters A through Z, and assign the pointers to the elements in the array." 

One thing you need to know is that the character A is actually a number. That's actually a number. It's truly a number. And a lot of people don't realize that. But you can actually do math against characters, because characters are just numbers. 

If you go to asciitable.com and you look at the capital letter A, you can see that the capital letter A is actually the number 65. Do you see that? So if I want to get to capital letter B, I would simply say 65 plus 1. If I want to get the capital letter C, it's 65 plus 2. Do you see how that works? 

I'm going to pop over here then, and I'm going to do a "for" loop, since I know that characters are numbers, and I'm going say that "i is 0, while i is less than 26 i plus plus curly brace, curly brace". And then, I'm going to say "my character pointers array at position i is going to be assigned the capital letter A plus i", like that. Because, remember, the capital letter A is 65. 

So if I say plus 0, because my first loop around I'm 0, it's going to put the capital letter A into the 0 slot. And then it's going to be increased by 1, so then it's going to say capital letter A plus 1. So that's going to be B. So B is going to be put into the 1 slot. 

And then, it's going to be capital letter A plus 2, which is C. So C is going to be put into the 2 slot, like that. It's going to load it all up for me with the characters from A to Z 

All right, got a bug. No big deal. Let's get it fixed. 

So you should be using char, and you should be using new. So that would be helpful if I actually did that. So we need to dynamically create a character, and the value of that character is going to be the A plus i, like that. 

So I'm dynamically creating the character. The initial value is going to be the capital letter A, the capital letter B, whatever loop I'm on. And then, one thing that you need to see up here is that the array is 26 pointers to characters. So by doing the new character, it's going to give me back a pointer to that character over on the heap. And that pointer will be held in this pointer array. 

So, "Allocate an array of 100 integers," integer pointers, "and assign." So allocate a hundred integer pointers, so "new int". And integer pointers, so "new integer pointer", like that, and they want a hundred of them like that. So this is 100 integer pointers, so that's why that pointer is there. "The resulting pointer to the appropriately declared variable ip array." 

Over here, I'm going to say "new", I'm going to say "int", because it's an integer pointer ip array, it's going to be assigned a new pointer array, integer pointer array. Then I'll put a semi-colon. "Allocate 100 integers, and assign the resulting pointers to the elements of ip array." 

Here, I want to say "for int i is 0, while i is less than 100", because that's how many I have, "i plus plus curly brace, curly brace". And then, I'm going to say "ip array at position i is going to be assigned a new integer". And what should the values be? Initialize each integer to negative 1. So I'm going to initialize each integer to negative 1. 

"The array must be a pointer array," that's why it needs to be an int pointer. "Each position needs to be a pointer," because when you create an integer on the heap using the new keyword, the new keyword gives you back a pointer. So this slot of the array must be a pointer. 

Here's one thing that I left off, and that is that this right here is a pointer array. And when you have that pointer array, when you put the new keyword on it, it says it's going to give you back a pointer to the pointer array. So I need to have a pointer to a pointer array. So that's a double pointer. 

So right here, I'm having a pointer to a pointer, like that. That's why you have to have a double pointer. And that's what I left off. 

So "int double pointer, a pointer to a pointer, is going to be assigned a new," which gives me back a pointer, "pointer array," like that. So the pointer array, the first slot in that array, is a pointer. 

When I put the new keyword on there, it's going to give me a pointer to that first slot, which is a pointer. So that's a double pointer. So that's why over here on this side, I need to have a double pointer. And that's how that works. 

All right, and that is 9.8. If you're in GSP, you're going to be using double pointers a lot. Your middleware, your physics engine, you're going to be using double pointers a lot, so be comfortable with it. This is 9.8.

[End of Audio]