MyProgrammingLab 9-7 Part 1 Transcript

Print

All right, let's do 9.7. So opening up 9.7. Double It's a function that takes one argument and returns no value. The argument is a pointer. The function doubles the values of the argument, stores it back where it was. Savings is an integer variable, so just a regular int that has been declared and initialized.

Write a statement that doubles the value stored in savings by invoking the function Double It. Now remember, Double It returns no value, all right? So we're going to say Double It. And then we're going to give it Savings, like that, semicolon.

But this right here is an integer. But the function takes one argument, and the argument is a pointer to the integer. So this is an integer. How do I get the address of that integer?

So all I do is simply go in front of it, and put an ampersand. And that's going to give me the address of the integer which can be held by pointer. So that, right there, is what satisfies the pointer to an int argument.

Triple It is a function that has one argument and returns no value. So, same thing here. And the argument is a pointer to an int, great.

And write a statement that triples of values in Penalty by invoking the function Triple It. For example, if the value in Penalty was 7 before the statement, now it's going to be 21. So the function is Triple It, and we're going to give it Penalty like that, but Penalty is an integer. But the function has a pointer to an int. And it has to be a pointer, because we want it to change.

And if you don't make it a pointer, or at least put an ampersand, then it will send the value and not the reference. So if you are more interested in that, what I just said, do a Google search. And just go C++, by value versus by reference. All right? And Google that and then read about it.

If you send it by value, it does not change. Because only the value is sent to the method. And when the method goes out of scope, it's no longer relevant in the main. However, if you send it by reference, if you send it the memory address, then it does change in the calling method.

So if you come up here to your Penalty, and you put an ampersand in front of Penalty, you are now sending it the memory address of Penalty. So you're sending a reference to the Penalty variable, so therefore it will change. And that's what makes it change. All right? So this will be the correct answer. So that Penalty does change.

And then also, the second fact that we have here is that the argument is a pointer to an int. So we need to get the address of Penalty so that it will be received by the pointer. Because remember, pointers can only hold memory addresses, and that's all.

All right, let's go ahead and clear this up. All right, Divide is a function that takes four arguments, returns no values. The first two arguments are ints, the last two arguments are pointers to ints. Because we want the last two arguments to change in the main. That are set by the function to the quotient and the remainder of the first argument and the second argument.

The function does not return a value. x and y are two ints that have been declared. q and r are two ints they have been declared.

Write s statement that sets the value of q to the quotient of x and y, being divided by each other, and the value of r to the remainder of x divided by r, by calling the Divide method. So we're going to call the Divide method, and we're going to give it x. And we're going to give it y. And then the third, the last two arguments are pointers. So we have to use ampersand, right? So we can get the address of, and they are pointers to ints that are set by the function to the quotient. So the quotient is the first one. And the remainder is the second one. And we have q and r, so q for quotient, r for remainder.

So I'm going to get the address of q, and the address of r, like that. So the Divide method is going to receive x and y, divide them. Then it's going to give the quotient, next. And then it's going to give the remainder. And because we've set these as pointers, we need to get the address of q and r. And they will be changed by the method. All right? That's how it should be done.

All right, minmax is a function that takes five arguments, has no return. The first three are inputs. The last two are set to the largest and the smallest. x, y, and z are inputs. Big and Small are our resulting values. And we want them to change, so that's why they were set to pointers.

Write a statement that sets the value of Big to the largest of x, y, and z, and Small to the smallest. So we're going to say, minmax is the method. And then it's going to be x, y, and z. And then we're going to send the address of Big, right? So we're going to set the address of Big, and we're going to set the address of Small. And then a semicolon.

So these three are going to be the inputs. And then we're going to send these two variables, here, by reference. We're taking the address of. So we're sending a reference to the variables. We're sending them by reference, so that they will change in the main method.

And the second two are pointers. So when we send them by address, by the memory address, the pointers will be able to receive the memory addresses.

All right, declare statement. You have a prototype. And you just want to create a prototype. So for this method, we do not have to write the whole method. We just have to write the prototype, and that's all. For a function called Double It, that is a single parameter that is a pointer to an int. So no return type, Double It, and then it's going to be an int pointer. And I'm just going to say input.

So there's our prototype. So the method is called Double It. It has one single input that is a pointer to an integer.

Write a statement that declares a prototype for a function called Triple It. That could be used just like this, OK? So no return type, and it's called Triple It. And the address of x is being sent, so it's going to be an int pointer. And I'm just going to use input, like that.

So this is going to receive a number, which is a pointer. It's actually a pointer is what we're going to receive, a memory address. This is a pointer to an int. And the method's going to Triple It. So the address, we're going to grab the address off of x. It's going to be received by the pointer. We're going to triple the content. And it's going to make it three times larger.

All right, statement declares a prototype. So we're doing a prototype again, so we don't have to write the whole function. We just want to write a prototype called Divide, that takes four arguments, no return value. The first are integers. The last two are pointers to integers that are set to quotient and remainder, by the first and second. The function does not return a value.

So we're going to say void, and then the method is called Divide. And then int first, int second. And I almost said int x and int y, and that would've worked just fine. But I'm going to use first and second.

And then the third one is a quotient. So this is going to be int pointer. And it's going to be called Quotient. I'm going to call it Quotient. And then int pointer remainder, semicolon.

All right, so the first two are and input variables. And they're just regular ints. The last two are output variables. I want them to change. So if I want them to change, then we need to be sending them by reference. Which means we need to be sending the address, the memory address-- not the value, not the content-- but the memory address. To guarantee that, we are doing these as pointers. So hit submit.

[End of Audio]