; Week3
Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>



  • pg 1
									CS1010: Programming Methodology
 Week 3: Top Down Design
       Able to analyse, design, and implement a program
       Able to structure a program into various functions
       Understand working with functions

       Chapter 5, Functions (Lessons 5.1 – 5.3)
       Chapter 3, Lesson 3.7: Math Library Functions

CS1010 (AY2010/2011 Semester 1)                              Week3 - 2
 Week 3: Outline (1/2)
   1. General
            Problem solving process (chart from Week 1)
            Ingredients in getting a program done: step-wise refinement +
                structure chart
   2. Demo: Week 2 Exercise #3 (Week2_Freezer.c)
            Problem solving
   3. Demo: Top-down Design
            Computing the weight of a batch of flat washers
            Incremental Refinement (some hierarchical chart)
            Top-down design (of program) with structure charts
   4. Exercise #1: Design a “simple drawing” program
   5. Exercise #2: Implement the “simple drawing” program

CS1010 (AY2010/2011 Semester 1)                                              Week3 - 3
 Week 3: Outline (2/2)
   6. Program as a collection of Functions to transform inputs to
               Syntax
               Precondition, postcondition
               Actual and Formal parameters
               Flow of control
               Function prototype
               Some math library functions
   7. Exercise #3
   8. CourseMarker

CS1010 (AY2010/2011 Semester 1)                                     Week3 - 4
 1. General
   Given a problem, how to proceed to reach a working program?
   Review week #2:

           Determine problem
                features             Analysis

                                                        Rethink as
              Write algorithm                           appropriate

               Produce code

        Check for correctness        Testing
           and efficiency

CS1010 (AY2010/2011 Semester 1)                                  Week3 - 5
 1. General
                      Analysis and Design

                        NO                         ( hierarchy of )

statement                                  sub-problems can                  YES
                                           be implemented ?              structure chart
                                                                                           & Testing

                                                                      Knowledge in
                       Knowledge in C
                                               Knowledge in           data structure
                       and its libraries
                                               algorithms             (mostly CS1020)

CS1010 (AY2010/2011 Semester 1)                                                                  Week3 - 6
 2. Demo: Week 2 Exercise #3
   1. Decide to have input (two integers), compute, and output
      (one float).
   2. Input is easy – use scanf
   3. Output is easy – use printf with %.2f
   4. Compute may also use
      library function(s)
   5. Put each input, compute,
      output in a function

 Change the formula to this:

                   4t 10
                T 9      20
                  t 2

CS1010 (AY2010/2011 Semester 1)                                  Week3 - 7
 3. Demo: Top-down Design (1/7)
   You work for a hardware company that manufactures flat washers.
   To estimate shipping costs, your company needs a program that
   computes the weight of a specified quantity of flat washers.

                                   rim area = (d2/2)2 – (d1/2)2

CS1010 (AY2010/2011 Semester 1)                                 Week3 - 8
     3. Demo: Top-down Design (2/7)
     -         To get the weight of a specified qty of washer, we need to know the
               weight of each washer
     -         To get the weight of a washer, we need its volume  density
     -         To get volume, we need its rim area  thickness
     -         To get the rim area, we need d2 and d1
     -         qty, density, thickness, d2, d1 should be given as inputs.


         qty            weight                         rim area = (d2/2)2 – (d1/2)2

               volume        density

 rim area           thickness

d2             d1
 CS1010 (AY2010/2011 Semester 1)                                                 Week3 - 9
 3. Demo: Top-down Design (3/7)
   - We define what the inputs and outputs are
   - Choose the identifier names and data types
   - Algorithm (view in words):
           1. Read in all the necessary inputs (qty, density, thickness, d2, d1)
           2. Compute weight of a single washer
                  2.1    Compute the area of the (small) hole using d1
                  2.2    Compute the area of the (big) circle using d2
                  2.3    Subtract the big area from the small area to get the rim_area
                  2.4    Compute volume = rim_area  thickness
                  2.5    Compute weight = volume  density
           3. Compute the weight of the specified number of washer = weight 
           4. Output the calculated weight
CS1010 (AY2010/2011 Semester 1)                                                          Week3 - 10
 3. Demo: Top-down Design (4/7)
   - Algorithm (view in some hierarchical chart)
                                                     Compute Total

                             Ask for all       Compute             Compute       Output total
                              inputs            Weight            weight x qty     weight

                                                Compute hole
                                                area (use d1)

                                                Compute big
                                   density?    circle area (use

                                                Compute rim

                                     d1?        volume (use

                                     d2?         weight (use

CS1010 (AY2010/2011 Semester 1)                                                                 Week3 - 11
 3. Demo: Top-down Design (5/7)
   - Structure Chart
            a documentation tool that shows the relationship among the sub-

                                            Compute Total

               Input : qty,       Compute Weight
                                                     Compute total   Output total
                 density,           of a single
                                                       Weight          weight
           thickness, d1, d2         washer

                                  Compute circle

CS1010 (AY2010/2011 Semester 1)                                                     Week3 - 12
 3. Demo: Implementation (6/7)

                                  To be continued…

CS1010 (AY2010/2011 Semester 1)                      Week3 - 13
 3. Demo: Implementation (7/7)

                                  gcc Week3_WashersWeight.c -lm

CS1010 (AY2010/2011 Semester 1)                         Week3 - 14
 4. Exercise #1: Design a “simple drawing” program (1/2)
   - Write a program to draw a rocket ship
      (which is a triangle over a rectangle, over
      intersecting lines), a male stick figure (a
      circle over a rectangle over intersecting
      lines), and a female stick figure (a circle
      over a triangle over intersecting lines)
                                                    rocket   male   female
   - No particular input needed, just draw the needed 3 figures
   - There are common shapes shared by the 3 figures
   - Algorithm (view in words):
           1. Draw Rocket Ship
           2. Draw Male stick figure
           3. Draw Female stick figure

CS1010 (AY2010/2011 Semester 1)                                     Week3 - 15
 4. Exercise #1: Design a “simple drawing” program (2/2)
  Design (Structure Chart):

                                                       rocket   male   female

  Note: Drawing triangle can further be broken down to drawing
        intersecting line plus drawing the base. Further subdivision
        depends on our need.

CS1010 (AY2010/2011 Semester 1)                                         Week3 - 16
     5. Exercise #2: Implement “simple drawing” program

   CS1010 (AY2010/2011 Semester 1)                  Week3 - 17
 6. Functions (1/6)
      A program is a collection of functions to transform input (if any)
       to output (if any)
      In general, each box in a structure chart, which is a sub-problem,
       gives rise to a function
      In mathematics, a function maps some input values to a single
       (possibly multiple dimensions) output
      In C, a function maps some input values to zero or more output
        - Zero output through         “void func ( … ) { … };”
        - One output through, e.g., “double func ( … } { …; return value; };”
        - More outputs through changing input values
              •    ‘&’ – ‘address of’ operator
              •    ‘*’ – ‘indirection’ operator; go to the address stored in the variable
                   following the * to get the/put a value at that address
      Return value (if any) from function call can (but need not) be
       assigned to a variable.
CS1010 (AY2010/2011 Semester 1)                                                             Week3 - 18
 6. Functions (2/6)
Syntax:                                          Example (in Week3_Sample.c):
function interface comment
ftype fname (formal parameter declaration list)
       local variable declarations
       executable statements
       // include return statements, if any

    Precondition: describes conditions that should be true before calling function.
    Postcondition: describes conditions that should be true after executing function.

CS1010 (AY2010/2011 Semester 1)                                                         Week3 - 19
 6. Functions (3/6)
Actual parameters are values passed to function for computation
Formal parameters are placeholder when function is defined.
          Matching of actual and formal parameters from left to right
          Scope of formal parameters, local variables is within the function only

          Arrows indicate flow of control between main and a call to a function
          Provide function prototype as function may be used before (compiler sees)
           its definition:

CS1010 (AY2010/2011 Semester 1)                                               Week3 - 20
 6. Functions (4/6)
 The complete
 program of

CS1010 (AY2010/2011 Semester 1)   Week3 - 21
 6. Functions (5/6)
Some Useful Math Library Functions (compiled with –lm option)
 function abs(x) from <stdlib.h>; the rest from <math.h> (see page 131)

CS1010 (AY2010/2011 Semester 1)                                Week3 - 22
 6. Functions (6/6)
      Use of functions allow us to manage a complex (abstract) task
       with a number of simple (specified) ones.
        -    This allows us to switch between abstract and go to specific at ease
             to eventually solve the problem.
      Function allows a team of programmers working together on a
       large program – each programmer will be responsible for a
       particular set of functions.
      Function is good mechanism to allow re-use across different
       programs. Programmers use functions like building blocks.
      Function allows incremental implementation and testing (with the
       use of driver function to call the function and then to check the
      Acronym NOT summarizes the requirements for argument list
       correspondence. (N: number of arguments, O: order, T: type)

CS1010 (AY2010/2011 Semester 1)                                              Week3 - 23
 7. Exercise #3
Write a program that calculates the speed of
sound (a) in air of a given temperature T (oF).
Formula to compute the speed a in ft/sec:

                                           5T  297
                                  a  1086

  Please spend some time to analyse and design the
   program before implementation (and testing).
  Bring your program to class next week.

CS1010 (AY2010/2011 Semester 1)                       Week3 - 24
        You will use CourseMarker to submit your lab
        Refer to “Getting Started” document:

        Read the above document, especially section 5
         “Submitting Your Programs Using CourseMarker” and
         try out before your discussion class this Friday.
        [Important!] Remember to bring along your
         CourseMarker password to your discussion class this

CS1010 (AY2010/2011 Semester 1)                                           Week3 - 25
 Summary for Today
        Today’s most important lessons
               Stepwise refinement to get structure chart
               Deeper understanding on working with functions
               Acquaintance with built-in functions
        Not in focus of this lecture but in book:
               Looping & condition (in other chapters which will be
                covered later)

CS1010 (AY2010/2011 Semester 1)                                Week3 - 26
        Discussion classes starting this week (Friday).
               Bring along your CourseMarker password
               Do Discussion Questions on module
                website (“CA”  “Discussion”) before you come for
                your discussion session:

        Revise Chapter 5: Functions (Lessons 5.1 –
        To prepare for next week’s lecture:
               We will do Selection statements (if-else, switch)
               Read Chapter 4 (Lessons 4.1 to 4.6) before you
                come for lecture

CS1010 (AY2010/2011 Semester 1)                                           Week3 - 27
                                  End of File

CS1010 (AY2010/2011 Semester 1)                 Week3 - 28

To top