English 字幕

← 01-08 Case Study Solution


Showing Revision 1 created 06/27/2012 by Amara Bot.

  1. Okay, now for the answer. Let's switch back to the code for a moment.
  2. We'll even put in an example just to see what it actually does.
  3. You should have already done this, but just in case you haven't
  4. let's run this and see what it does. We put in 4 and 5. It came out with 20.
  5. That actually should be enough for you to make out which of the answers
  6. that I gave you is correct.
  7. It's clearly computing the product of these two, at least in this example.
  8. But why in general is that what it's doing?
  9. Let's actually take a look at the code and see what happens.
  10. We start off with a and b, and we leave them alone, but we put copies of them into x and y.
  11. We have a z variable that we initialize to 0.
  12. Then what we're going to do is we're going to keep detrimenting x,
  13. keep subtracting 1 from x until it hits 0.
  14. Each time we subtract 1 from x we add y into z.
  15. We're starting off with z as 0, and we add y to it--how many times? X times.
  16. Z at the end of this is x added to itself x times--x * y.
  17. It is, in fact, computing the product of these.
  18. Now, that was kind of an informal argument.
  19. Can we be a little bit more formal and mathematical to show
  20. why it is that what this is computing is the product of a and b?