English subtitles

← 2 - Travel Times SOL

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

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