-
Title:
2 - Travel Times SOL
-
Description:
-
Okay.
I'm going to just jump right in and
-
hand simulate what's going on here.
-
These first three lines we just
declare three variables and
-
set them to initial values,
so I'm going to do that.
-
Okay, so
that's what that would look like.
-
And then here in display,
I'm saying display this arithmetic here.
-
Let's look and
see what the values of day 1, day 2, and
-
day 3 are at this point in the program.
-
Day 1 is 15.
-
Day 2 is 22 and day 3 is 18.
-
Now, this might seem a little bit silly
that I went off here to the side and
-
started keeping track
of these variables, but
-
as the programs get more complicated and
variables kind of change and
-
get modified in this space, it's really
important to be keeping track of things.
-
Anyways, the key insight for
-
this problem is that you're doing
both addition and a division here.
-
Now, if you remember from when Katherine
was talking about order of operation,
-
division always comes before addition.
-
Meaning the first thing that's going to
happen is this division right here.
-
So, 18 gets divided by 3, which is 6.
-
And then we have 15 plus 22 plus 6,
which equals 43.
-
So, it's going to print out 43.
-
So, is that correct?
-
Well, what we're trying to
do is find the average.
-
Finding the average involves
adding up all the numbers and
-
then dividing by the number
of numbers you have.
-
But we did the addition too early,
-
we do it before we've added
up these three numbers.
-
Really, it should be 15 plus 22
plus 18 then divided by three.
-
So 43 is not the correct answer.
-
And to make this code
do the correct thing,
-
you should be surrounding these
three variables with parentheses.
-
Then, you'll add 15 plus 22 plus 18.
-
And then divide it by 3, which will give
you the correct answer of about 18.3.