MyProgrammingLab12-2 Part 2 Transcript

Print

So I'm going to grab this code right here, and do a Control-C on it. I'm going to go over to my code lab, and I'm going to pop Control-V. Just to clean it up, I'm going to get rid of my tab here. It would accept it without me getting rid of those tabs, but I want it anyway. So then I hit Submit. 

So this one right here says, "You have three variables that have already been declared and initialized," so they already have values, in addition, "and you want to print them on the same line, separated by one single space, in such a way that scientific notation is avoided." So we do fixed. And then, "Each number should be printed with five digits of precision, and then it should look like this." So I'm going to say "cout". and I want to avoid the scientific notation, so I'll say "fixed". And then five digits of precision. 

And then, let's see what else they wanted. So "A", and then they want a spacebar. So we'll put a spacebar. And then, I can say "B", and then I can put a spacebar. And then I can say "C". 

So once you set the precision, it sticks. So you don't have to reset the precision. So you just say "fix setprecision" one time, and then it will maintain that until you change it. 

I'm going to go ahead and just do a Control-C on this to show you what I'm talking about. And then over here, I'm going to pop it here, Control-V. And then, this right here is going to be "A", and then they said it was initialized. So four, one point, and then whatever. 

And then, "B", initialized, and then I'll say 13.13. And then I'm going to say "C", and it's initialized. So I'm going to say nine point, now watch this. I can put a whole bunch of numbers. A whole bunch numbers like that, and it won't matter because my setprecision is going to round it to whatever I say here. So I'm going to get rid of that line real quick. So I'm setting the precision to five, and then I'm separating each number by a spacebar. So now when I execute, C++ pops this for me. 

Not quite what I was expecting. So 41, 13, 9, oh, that's too funny. Because it's an int. Integers can only hold whole numbers, and that's it. So they cannot hold decimal places. So if I drop these into ints, it just lops off, it just chops off the decimal places. You'll notice it does not round because this nine is 9.9. It doesn't matter. It is does not round it. It chops it off, it lops it off. 

I'm going to make it a double, and then I'm going to execute. And then notice that this is three decimals, and the extra two were put to zeroes. And then this right here, three extra zeroes were added because I'm setting the precision to five. And then this right here, is a big huge number, and it doesn't matter, it got rounded off to five decimal precision. So that's how the setprecision works. And you will use setprecision a lot in production code. So you want to be comfortable with it. 

So this one right here, "The variables have already been declared and initialized." They want a "15 position field, in such a way that scientific notation is avoided." And then, "Each number should have five digits of precision." So I'm going to go ahead and say "cout". And then "fixed", and then "setprecision". And setprecision, how many, five? Five digits of precision. 

And then "setw". The set width is going to be the column width, and they want 15. So I'm going to say 15. Now one thing to know, also you can say right or left. So I want to remind you all that you can say right or left. So I'm going to say right, even though that's the default for numbers. 

So now I want to also tell you that the setw only applies to the next column. And that's it. So you actually have to say "setw" each time, because each column could be a different width. So I'm going to do "setw(B)", and then I'm going to "setw" for C. And now we'll do an "endl". 

So just remember you have to set the column width for every single column. The precision will stick, no problem. But the column width does not. So make sure that you set the column width for each column. 

So now they want the same thing. Let's see, "nine position field," and actually this is easier. They just want you use the set width. So I'm going to go ahead and say "cout", and then I want it to be right-aligned. 

I want it to be right-aligned because you see the spaces. The Xs are representing spaces. So they don't actually want Xs to appear, these are just representing spaces. I want that many spaces, and then I want the number to show up, and then I want that many spaces, and then I want the number to show up. 

So you can see that the number in the column because the column is the entire thing, the spaces plus the number. That's the column. They want that column to be, let's see, nine wide. Let's see. Yeah, they want the column to be nine wide. 

So that means that they have these. If it's 27, there's the first one. Then there's a second column, and then there's a third column. So I'm going to write a line, because you can see that the spaces are on the left, and then I'm going to set the width to nine wide. 

And then I'm going to show the first one, which is K. And then I want to set the width again to nine. And then I want to set the second one. And then I want to set the width to nine. And then I'm going to send the next one. And then we'll send it, "endl", like that. So each column is nine wide. And I'm sending one variable after another, and then and "endl" at the end. 

And that is 12.2.

[End of Audio]