WEBVTT
00:00:00.510 --> 00:00:02.100
Okay.
I'm going to just jump right in and
00:00:02.100 --> 00:00:03.630
hand simulate what's going on here.
00:00:05.560 --> 00:00:08.090
These first three lines we just
declare three variables and
00:00:08.090 --> 00:00:10.610
set them to initial values,
so I'm going to do that.
00:00:11.670 --> 00:00:13.660
Okay, so
that's what that would look like.
00:00:13.660 --> 00:00:18.070
And then here in display,
I'm saying display this arithmetic here.
00:00:18.070 --> 00:00:20.491
Let's look and
see what the values of day 1, day 2, and
00:00:20.491 --> 00:00:23.110
day 3 are at this point in the program.
00:00:23.110 --> 00:00:24.044
Day 1 is 15.
00:00:26.440 --> 00:00:30.270
Day 2 is 22 and day 3 is 18.
00:00:30.270 --> 00:00:34.300
Now, this might seem a little bit silly
that I went off here to the side and
00:00:34.300 --> 00:00:36.540
started keeping track
of these variables, but
00:00:36.540 --> 00:00:39.960
as the programs get more complicated and
variables kind of change and
00:00:39.960 --> 00:00:44.770
get modified in this space, it's really
important to be keeping track of things.
00:00:44.770 --> 00:00:46.110
Anyways, the key insight for
00:00:46.110 --> 00:00:50.400
this problem is that you're doing
both addition and a division here.
00:00:50.400 --> 00:00:53.700
Now, if you remember from when Katherine
was talking about order of operation,
00:00:53.700 --> 00:00:55.980
division always comes before addition.
00:00:55.980 --> 00:00:59.220
Meaning the first thing that's going to
happen is this division right here.
00:00:59.220 --> 00:01:01.820
So, 18 gets divided by 3, which is 6.
00:01:01.820 --> 00:01:06.500
And then we have 15 plus 22 plus 6,
which equals 43.
00:01:06.500 --> 00:01:08.610
So, it's going to print out 43.
00:01:08.610 --> 00:01:10.840
So, is that correct?
00:01:10.840 --> 00:01:12.740
Well, what we're trying to
do is find the average.
00:01:12.740 --> 00:01:15.920
Finding the average involves
adding up all the numbers and
00:01:15.920 --> 00:01:18.208
then dividing by the number
of numbers you have.
00:01:18.208 --> 00:01:19.790
But we did the addition too early,
00:01:19.790 --> 00:01:22.630
we do it before we've added
up these three numbers.
00:01:22.630 --> 00:01:27.210
Really, it should be 15 plus 22
plus 18 then divided by three.
00:01:27.210 --> 00:01:28.780
So 43 is not the correct answer.
00:01:30.020 --> 00:01:31.790
And to make this code
do the correct thing,
00:01:31.790 --> 00:01:34.780
you should be surrounding these
three variables with parentheses.
00:01:34.780 --> 00:01:37.210
Then, you'll add 15 plus 22 plus 18.
00:01:37.210 --> 00:01:44.449
And then divide it by 3, which will give
you the correct answer of about 18.3.