Docsity

Top-Down Program Design: Understanding Functions and Algorithms - Prof. Mikhail Nesterenko, Study notes of Computer Science

Toggle sidebar
bg1
Ch 6 / Foil 1
lfunction
npredefined, programmer-defined
larguments, parameters
lreturn value
lfunction call, function invocation
lfunction description
ndefinition
–head, body
nprototype
–expanded form, abbreviated form
llocal variables, global variables, scope
lcall-by-value
lblack box principle, information hiding, procedural abstraction
What is?
Top-down program design
debugging functions
Ch 6 / Foil 3
lhumans can only handle a certain (relatively low) level of
complexity
la program longer than 50 (20?) lines is already probably too
complex
ltop-down design - a method to decrease complexity a
programming task by by breaking it down into a few simpler
relatively independent subtasks (other method names -
stepwise refinement and divide and conquer)
lif a subtask is still complex - further subdivide
leach simple subtask should be coded by a separate function
or other separated piece of code
ladded benefit - separate people (or separate teams) can
work on different subtasks
lblack box principle is important
Top-down design Ch 6 / Foil 4
lproblem: a large “economy” size pizza is not always the best
buy:
npizza sizes are given as a diameter (in inches)
nbetter buy has lower price per square inch
nmost people cannot easily estimate it and the vendors
are making an easy buck
lprogram specification: design a program that compares two
pizzas and determines which is a better buy
ninput: the diameters and total prices of the pizzas to
compare
noutput: cost per square inch for each pizza and which is a
better buy
Example: buying pizza
Ch 6 / Foil 5
lWe will use top-down design.
lThe subtasks are:
1. Get the input for both pizzas
2. Compute the price per square inch for first pizza
3. Compute the price per square inch for second pizza
4. Determine which is the better buy
5. Output results
lsubtasks 1 and 4 and 5 are straightforward
lsubtasks 2 and 3 are very similar - the arguments are taken
and one value is computed - good case for a function, let’s
call it unitprice
Analysis of the pizza problem Ch 6 / Foil 6
lA sequence of steps is called an algorithm
lalgorithm for unitprice:
1. Compute the radius of the pizza
2. Compute the area of the pizza by formula: πr2
3. Compute (price / area) and return it
lpseudocode - a mixture of c++ and ordinary English - makes
your algorithm more concrete
radius = one half diameter
area = π* radius * radius
return (price / area)
lwhat arguments does the function need? price, diameter
lwhat value should it return? unit price per square inch
unitprice for pizza problem
pf2

Partial preview of the text

Download Top-Down Program Design: Understanding Functions and Algorithms - Prof. Mikhail Nesterenko and more Study notes Computer Science in PDF only on Docsity!

Ch 6 / Foil 1

l function n predefined, programmer-defined l arguments, parameters

l return value l function call, function invocation l function description n definition

  • head, body n prototype
  • expanded form, abbreviated form

l local variables, global variables, scope l call-by-value l black box principle, information hiding, procedural abstraction

What is?

Top-down program design

debugging functions

Ch 6 / Foil 3

l humans can only handle a certain (relatively low) level of complexity l a program longer than 50 (20?) lines is already probably too complex l top-down design - a method to decrease complexity a programming task by by breaking it down into a few simpler relatively independent subtasks (other method names - stepwise refinement and divide and conquer ) l if a subtask is still complex - further subdivide l each simple subtask should be coded by a separate function or other separated piece of code l added benefit - separate people (or separate teams) can work on different subtasks l black box principle is important

Top-down design

Ch 6 / Foil 4

l problem: a large “economy” size pizza is not always the best buy: n pizza sizes are given as a diameter (in inches) n better buy has lower price per square inch n most people cannot easily estimate it and the vendors are making an easy buck l program specification: design a program that compares two pizzas and determines which is a better buy n input: the diameters and total prices of the pizzas to compare n output: cost per square inch for each pizza and which is a better buy

Example: buying pizza

Ch 6 / Foil 5

l We will use top-down design. l The subtasks are:

  1. Get the input for both pizzas
  2. Compute the price per square inch for first pizza
  3. Compute the price per square inch for second pizza
  4. Determine which is the better buy
  5. Output results l subtasks 1 and 4 and 5 are straightforward l subtasks 2 and 3 are very similar - the arguments are taken and one value is computed - good case for a function, let’s call it unitprice

Analysis of the pizza problem

Ch 6 / Foil 6

l A sequence of steps is called an algorithm l algorithm for unitprice:

  1. Compute the radius of the pizza

2. Compute the area of the pizza by formula: π r^2

  1. Compute (price / area) and return it l pseudocode - a mixture of c++ and ordinary English - makes your algorithm more concrete radius = one half diameter

area = π* radius * radius

return (price / area) l what arguments does the function need? price, diameter l what value should it return? unit price per square inch

unitprice for pizza problem

Ch 6 / Foil 7

l start your coding by specifying in comments what your subtasks are doing l code the tasks l if the program compiles - and produces answers - it does not mean it is correct n get your calculator and check n if your program produced correct answer once it may still be incorrect - ran more extensive test l it may still be incorrect

Coding and testing

Ch 6 / Foil 8

l there is no way to prove that the program is correct! l A test either shows an error or demonstrates that the program behaves correctly under the particular conditions of the test l with more successful tests we gain confidence but we can never be 100% sure l How to test successfully? n Important condition - input values

  • use various (maybe not necessarily A LOT just different) input values
  • check boundary conditions a boundary condition is when the behavior of program changes - what happens when the the sizes of pizza are the same? What if the prices per inch are the same l what if there is an error? Test demonstrates the presence of error not where it is

More testing

Ch 6 / Foil 9

l Part of top-down design strategy - each function has to be designed, coded and tested as a separate unit l How do we do it if functions use other functions - they are not independent l wrapper - a simple function that accepts input from user and passes it straight to the function tested - you can test function under various inputs n what would be a wrapper around unitprice? l stub - a simple function that always returns reasonable (not necessarily correct values) - can be used instead of a function to test the rest of program n what would be a stub for main?

Testing functions

stubs and wrappers

Ch 6 / Foil 10

l Program errors: n syntax - caught by compiler n run time ( bugs/logical errors ) - bugs that cannot be caught by compiler l Debugging - the process of eliminating run-time errors. Usually takes 2 to 10 times more time than writing code l The most comprehensive way to debug your program is using debugger (gdb) n you can trace your execution through (over the functions) go inside the function n locate the exact place where program fails n see and change the values of variables

Debugging