Simula tion and topdown design Chapter 9 What

  • Slides: 16
Download presentation
Simula tion and top-down design Chapter 9

Simula tion and top-down design Chapter 9

What is simulation? Simply put, simulation means using computing processes to simulate real-world situations

What is simulation? Simply put, simulation means using computing processes to simulate real-world situations to obtain information that would be difficult to get or would be obtainable only over a long period of time.

Monte Carlo algorithms Because we want simulations to reflect the unpredictability of much of

Monte Carlo algorithms Because we want simulations to reflect the unpredictability of much of what happens in the world, the simulations should include “chance” probabilities. In programming language terms, we want random generation functionality in our simulation.

Pseudorandom numbers A seed value is given to a function which produces a return

Pseudorandom numbers A seed value is given to a function which produces a return value; that value in turn is passed back to the same function so that a different return value is generated. Python supports a module called random which does just this. The seed value is the date and time the function was launched, so the pseudorandom numbers produced are always unique. Always import the library: import random

randrange() The randrange() function creates a random integer between any two values given as

randrange() The randrange() function creates a random integer between any two values given as parameters. randrange(1, 10) will return a random integer between 1 and 9. A third parameter can give a multiple of a value. For example, randrange(1, 100, 2) will produce a pseudorandom integer between 1 and 99 which is a multiple of 2— that is, an even number.

random() takes no parameters. It produces a float number between zero and up to

random() takes no parameters. It produces a float number between zero and up to but not including 1. 0. When importing the module, you may want to take the shortcut of specifying which function you want to use: from random import random This prevents the awkward code random()

Pseudocode According to the Dictionary of Computer and Internet Terms, pseudocode is “an outline

Pseudocode According to the Dictionary of Computer and Internet Terms, pseudocode is “an outline of a computer program, written in a mixture of a programming language and English. Writing pseudocode is one of the best ways to plan a computer program. ” As we discuss top-down design in the ensuing slides, we will actually be working in a kind of pseudocode.

Top-down design simply means you start with the big problem and break it into

Top-down design simply means you start with the big problem and break it into smaller tasks. It’s a design strategy that complements the programming strategy of modularization well. Your main() module will contain all the subroutines or submodules or subtasks that comprise the whole problem. Hence the main() module is the top level.

Separation of Concerns This again is closely connected to modularization. The main() module only

Separation of Concerns This again is closely connected to modularization. The main() module only cares about what it has to provide to functions and what is returned from functions. At this stage, all we need to know are the parameters for the functions main() will invoke and what values main() will get back in return. The name, parameters and return values of functions invoked by main() are the interface or signature of the functions.

Structure or module hierarchy charts We can illustrate these features in a structure or

Structure or module hierarchy charts We can illustrate these features in a structure or hierarchy chart. Parameters, function names, and return values are listed—but no more detail than that. main() Get_input() Process_input() Display_outcome()

Abstraction The top-level design in the preceding slide could be applied to almost any

Abstraction The top-level design in the preceding slide could be applied to almost any program; in fact, it works well as a very general outline of our final program (where we ask users to select items to buy, calculate the bill, and print an invoice). At this level, the details are not important; we’re just establishing the overall structure of the program. The design principle of ignoring details and concentrating on important features at different levels is called abstraction.

Second- and third-level design As we work through our pseudo-code, we will repeat the

Second- and third-level design As we work through our pseudo-code, we will repeat the same process for each function which main() calls. As we break each subfunction down into its inherent tasks, or subsubfunctions, we are detailing secondand third-level designs.

Bottom-up implementation When we start actually coding in Python, it is best to begin

Bottom-up implementation When we start actually coding in Python, it is best to begin at the bottom of our structure/hierarchy chart. For example, in the very generic structure chart on slide 10, it would make sense to start coding the function get_input(). For our final project, that function might include getting the user’s name, address, email, product selection, quantity, etc. Each of those might be second -level functions. Beginning the coding with these lower functions is called Bottom-up Implementation.

Prototyping Prototyping design means you implement part of a program, test it with users,

Prototyping Prototyping design means you implement part of a program, test it with users, then add more features if that prototype passes muster. For our final project, you might prototype a GUI which just gets user contact info and prints it. The next prototype might be getting the product selection and calculating a total. In a sense, earlier projects in this class are prototypes of the final assignment.

Spiral development When we code, test the prototype, add new features in code, test

Spiral development When we code, test the prototype, add new features in code, test the prototype again, etc. , we are using spiral development. Each step in the process depends on the successful completion of the preceding type. Spiral development does several things: >>it modularizes the development process >>it ensures continuous user/client input >>it controls error by taking a step-by-step approach

Workshop Look at Exercise # 12 on p. 293. In groups of three, write

Workshop Look at Exercise # 12 on p. 293. In groups of three, write a three-level structure chart that outlines what the code would look like to solve the problem posed in this question.