MyProgrammingLab 9-7 Part 2 Transcript

Print

All right, "Write a statement that declares a prototype," again, we're doing a prototype, "for a min-max. It has five parameters. Three are integers. Two are largest and smallest." All right, so void min-max. Int first, int second, int third, int pointer, because I want it to change. So I am going to be using it as a pointer. And this is going to be largest and then ent pointer smallest semicolon. Now I'm putting a semicolon instead of a body because they just want a prototype. 

"What's the difference between a prototype and a method?" The prototype has a semicolon. The method does not have a semicolon, and instead it has a body with all of the code inside of it. Okay? So that's the difference between a method and a prototype. 

"Write definition out." Right here they want a definition. So they want the full-blown function. They want a definition for the function. "Min-max has five parameters. The first three are integers. The last two are pointers. The last two are set by the function as largest and smallest. The function does not return a value." 

So we want the values to be set, largest and smallest, so therefore we're going to do them as pointers. All right? So here we go. No return type. So I'm going to say "void," and then its min-max. It's going to take in five, so I'm going to say "int first, int second, int third, int pointer largest, int pointer smallest." And that's my five. 

Ok, now we need to do a body because it wants the definition. It does not want just a prototype. It wants a full-blown definition. So, now, how do we calculate this? Well, first of all, we have to find out what is the largest one. So I'm going to say, "if the first is bigger than the second, and, at the same time, the first is bigger than the third, well then that means that the largest must be the first." 

But wait a minute, largest is a pointer, so therefore I have to dereference the pointer, like that. Okay? So I'm going to say, "the variable at this pointer is going to be assigned first." So I'm going to dereference the pointer, and at that position I'm going to assign the value of first. Then I'm going to say "else if the second is bigger than the first, and," and that's how we say and in programming, right, "and the second is bigger than the third, then we're going to dereference largest, and we're going to assign the second," because that is our largest. Now I'm going to say, "else, if the first is not the largest, and the second is not the largest, then I guess the third must be the largest." So I'm going to say "largest is going to be assigned third," like that. 

Now I need to have a second "if" block. And I'm going to say, "if the first is smaller than the second, and at the same time the first is smaller than the third, then that means that the smallest must be the first because the smallest is smaller than the other two." 

Now I'm going to say, "else if the second is smaller than the first, and at the same time the second is smaller than the third, well then I guess the smallest must be the second. Now if the first is not the smallest, and the second is not the smallest, then the smallest must be the third." Like that. So I'm going to go ahead and grab this code. I'm going to do a Control-C on it to copy. Now I'm going to go ahead and submit. 

Now one thing I want to show you, if we go over to the work area. If you don't like first, second, and third, you can use whatever variables you want. So x, y, and z, and then I can say "big," and then I can say "small." And then, but if I use x here, then I need to use x throughout my application. And then y is going to be here and all my seconds are actually going to be y, and then this first here is going to be x, right? And then this is going to be y and then y, y, y, y, and the third is z instead. 

So I guess what I'm showing you is when you're creating these, you can use different variable names. Not a problem at all. So big is my largest. So address of the big, big, big. So it's the exact same logic. I'm just using different variable names. Like that. Okay, so, oh third. I didn't change that one, did I? Okay, that one should be a z. Now looking at all of them, it looks like I got them all. So if I submit this, it's still correct. Do you see that? 

Now in addition, you can also add curly braces. So if you want to separate your if block and have curly braces for each section, that's good. So you can add your curly braces, no problem. So using different syntax is fine, as long as the code works. And if the code doesn't work, then that's when MyProgrammingLab is going to come in and tell you. It's going to give you some hints, give you some assistance. So you can definitely try stuff and see how it works. And you can see that my syntax can change, no problem, and as long as it works, as long as it's effective code, lab likes it. 

Let's go to the next one. "Write a definition for a function called Double It that doubles the values." So it wants a definition. So it actually wants us to write the definition. Se we are going to say "void, double it." And it's going to take one single input, and that input needs to be the address. So we're going to do a pointer and then it wants us to double it. So I'm going to go ahead and say the content of the pointer, the content at the pointer, is going to be assigned what the content of the pointer used to be times two. That's going to double it. So remember, the right side is done first, and then once the calculation is done, then it's assigned into the left side. So the variable at that pointer address is going to be assigned what the variable at that pointer address used to be times two, so that's going to double it. 

Let's go to the next one. And this one right here is basically the same thing but tripling. So we're going to go ahead and say "void, triple it." And again, I need to use an address so I'm going to do a pointer. If I use the address it will be sent by reference, and therefore it will change. If I don't use the address, it's going to be sent by value, and it will not change in the main. So I'm going to do my input. And again I'm using the word input. You can use x, you can use anything that you want. So, for the name you can use anything that you want, no problem. So now I'm going to say "dereference the pointer and then assign to it what it used to be times three," and that's going to triplet it. And that is 9.7.

[End of Audio]