In Class: Week 7 Friday:
Top-Down Design Example

If you have not already done so, create a week07 subdirectory in your cs21/class directory:

$ cd
$ cd cs21/class
$ mkdir week07
$ cd week07
Then copy of the file from my week07 directory:
$ cp ~newhall/public/cs21/week07/ .

Top Down Design

Top Down Design is a problem solving technique where:
  1. Start with General Description of Problem
  2. Break it into several high-level steps
  3. Iteratively break the steps into smaller steps until you have steps that are easy to solve
Top-down design is a lot like creating a paper outline for a large paper where you start with the main sections, and iteratively refine each part with more and more detail until you are ready to start writing the paper.

When you use top-down design, your resulting program's structure should closely match that of the steps: the main function should have calls to a few high-level functions, usually one for each high-level step; high-level functions have calls to functions that implement sub-steps of the high-level step; and so on.

Program functions also come from recognizing part of the code (or recongizing steps in the algorithm) where that are similar to other parts and generalizing that functionality into a function.

Iterative Refinement

When writing a large program, programmers use Iterative Refinement: do some top-down design, write function stubs for this part of code and maybe some implementation, then test. Iteratively, add more functions, and perhaps refine some of the steps using Top-Down design, and test, and so on. The idea is to write some code, test it, then write a little more code, and test it before writing even more. Usually, I write a function, then test it, write another function, test it, ... This way if I'm careful about testing, I know that if there is a bug in my program it is with the new code I've just added.

Function Prototyping

We often use prototyping to just put in function stubs so we can test the whole program's flow of control without having to have a full implementation. For example, here is a stub for a function to compute square root (it doesn't actually do anything but print out a message with the parameter value and return some bogus value, but I can call it from other parts of my program):
def squareRoot(num):
  This function computes the square root of a number.
    num: the number 
    returns: the square root of num

  print "inside squareRoot num is", num

  # TODO: implement this function

  return 1

Let's try it out...

We are going to try applying Top-Down Design and Iterative Refinement to write a program that computes the winning percentage of the game of Craps by simulating some number of games and keeping track of the number won.

The game of Craps is played as follows:

A player rolls a pair of six-sided dice.

If the initial roll is 2, 3, or 12, the player loses.

If the initial roll is 7 or 11, the player wins.

Any other initial roll causes the player to "roll for point". This means that the player keeps rolling either until s/he re-rolls the initial value (a win) or rolls a 7 (a loss).

Let's write a program to simulate multiple games of craps and estimate the probability that the player wins.

First Step of Top Down Design:

  1. Input: Get the number of games to play
  2. Compute: Play number of games of craps
  3. Output: print out the number of games won and the winning percentage