MyProgrammingLab 9-2 Part 3 Transcript

Print

And instead of doing it like this, which wipes out our data, I'm going to create a temporary variable, and I'm going to put the content of XP into that variable. And then I'm going to copy YP into XP, and then into the content of YP, I'm going to copy the value from that temporary variable like that.

And they give you a hint right here by saying declare any necessary variables. So they're giving a hint about that. All right, so now I hit Submit, and that's how it works. Oh, I've got an error. "Your code contains a runtime error. That means the code is correct from a language point of view, but it's doing something illegal."

All right, so accessing invalid memory locations, divided by 0, or something like that. All right, so let's go look at it very carefully to see what happened. Creating Ent temp is being assigned the content of XP. And then I'm saying the content of XP, the variable of XP is assigned the content of YP. And then the content of YP is being assigned whatever's inside of temp, which should be up here.

I don't see anything wrong with that. Let me Run again. All right, let's read the instructions a little more carefully. Variables have been declared. Write the code to exchange the values of these two variables, so that after the swap, XP points to what YP originally pointed to, and vice versa.

Oh, so they don't want us to stop the content. Instead, they want to swap what it points to. So they want to swap the memory addresses. OK, that's going to make our life even easier, because all we have to do is create a pointer to an integer and then put the memory address into that pointer. Then to the memory address, we're going to assign the memory address of YP. And then YP is going to be assigned the memory address of temp.

Got it. All right, so they don't want us to swap the content that the pointers are pointing to. Instead, they want us to actually swap the pointers, so swap what XP points to with what YP points to. All right, so what they're actually asking for is they actually want us to do this. They want XP to point to second, and they want YP to point to first after the swap.

So to do that, then I'm going to make this into a pointer, and then this right here I just do straight off. So I just take the memory address, the hexadecimal address, and I put into temp, and I take the hexadecimal address of YP and I put it into XP like that. So we're switching. So with this right, here first and second are not going to change. So we're not changing first and second. Instead, we're actually changing XP and YP and what they're pointing to.

So let me show you what I'm talking about on here. We're going to say, what is XP? What is XP? And then what is YP? And then what is YP? All right, so now I'm going to grab these two lines, I'm going to do a Control-C on it to copy. And then I'm going to come up here before the swap, and then I'm going to do a Control-V.

And then, just so we can see the difference, I'm going to do a blank line. So cout endl is a blank line, right? Because that just drops us down to the next line. All right, so now I'm going to run it to show you what it looks like. And check this out. You can see that initially, XP is pointing to this memory address. And you can see it ends with 58. All right, YP is pointing to this memory address, which ends with 4C. So this is a hexadecimal number, it's a memory location.

Now what does 58 point to? 58 points to the memory location of first. So first is a variable that has 27 inside of it. It's actually located on your RAM, your memory chip. When you buy memory for your computer and slide it into the slot, that's your memory. And that memory then has a memory address for each position, every variable that's in there. So XP is initially pointing to wherever a memory, the first is located. And YP is pointing to wherever a memory, the second is located. Because that's what we assigned right here.

So now here at this point, I'm taking the memory address and put it into temp. Then XP is being overwritten with the memory address of YP, and then YP is being overwritten with the memory address of temp. So now after we do that swap, you can see XP is now pointing to 4C. And what is 4C? 4C is second. And then YP is pointing to 58. What is 58? 58 is first. So we have flipped where they point like that, and that's what they're after.

So let's go ahead and flip back on over. And we're flipping the hexadecimal numbers. We're flipping what they point to. So now I'm going to hit Submit. There we go. All right. So let's go to our last one, and then this one right here says the variables have both been declared, and they're pointing to things. But now it contains the integer value originally contained--

"Write the code to exchange the two integers so that after the swap, XP still points to the same location, but that location now contains the integer value originally contained in the location pointed to by Y, and vice versa." So in this exercise, you're swapping the integers, not the pointers. So this is what we did last time.

So all we have to do here is create a temporary variable and then assign to it the content of XP. Then we're going to assign to the content of XP whatever's in YP, the content of YP. And then we're going to assign to the content of YP what was in XP originally, which we now have placed into temp.

So in this case, we're actually swapping the content. So in the last problem, they had us swap the memory addresses, but now they're having a swap the content. So I'm going to hit Submit, and we have it correct. All right, that's 9.2.

[End of Audio]