cins 113

14
CINS 113 COMPUTER LOGIC Chapter 9: Structured Program Design Sections 9-3 thru 9-5

Upload: phoenixdiablo

Post on 22-Oct-2015

24 views

Category:

Documents


1 download

DESCRIPTION

9-3 thru 9-5

TRANSCRIPT

CINS 113 Computer LOGIC

CINS 113Computer LOGICChapter 9: Structured Program DesignSections 9-3 thru 9-5

Welcome to CINS 113, Computer Logic, Chapter 9, Structured Program Design, Sections 9-3 thru 9-5.

1Section 9-3: Program Design Concepts (Page 310)BackgroundEarly programming was confusing, few standardsStructured programming introduced in 1964:Any algorithm can be written using only 3 control structuresTop-down developmentTakes a big task and breaks it into smaller tasksModular programmingCreating functional modules that interact efficiently

Programming in the early days (the 1940s and 1950s) was confusing, as it was a new field and there were few standards. A methodology called structured programming was introduced in 1964, stating that any algorithm can be written using only 3 control structures. In this chapter, you will also learn about top-down development, which takes a big task and breaks it into smaller tasks, and modular programming, which concerns the creation of functional modules to do those small tasks, which operate and interact efficiently.2Structure Theorem(Page 311)States that any algorithm can be written using only 3 control structures:Sequence (steps in order, no variation)Selection (steps are executed or not, based on the evaluation of a condition)Repetition (repeating of a set of steps, based on the evaluation of a condition, also called looping)Interaction of structuresStructures can contain other structuresStructures can be contained within other structuresStructures cannot overlap other structures

The concept of writing algorithms from only 3 control structures is called the structure theorem. The three control structures are: Sequence (where steps are done in order, with no variation), Selection (steps are executed or not, based on the evaluation of a condition), and Repetition (the repeating of a set of steps, based on the evaluation of a condition, also called looping). Programs are made up of many interacting structures. Structures can contain other structures, or be totally contained WITHIN other structures, but structures cannot overlap.3SeQuence Structure(Page 312)Easiest to understand: steps in orderNo IFs, ANDs or BUTs: just do it!Example:LET a = 1LET b = 2LET c = 3LET sum = a + b + cLET average = sum/3OUTPUT average

The Sequence structure is the easiest to understand. Do the steps in order, with no IFs, ANDs, or BUTs. Just do it! The statements here are an example of the Sequence structure.4Selection Structure(Page 312)Most common form is IF-THEN-ELSEExample:IF purchasePrice > 1000 THEN LET discountRate = 0.10ELSE LET discountRate = 0.05END IFVariation is IF-THEN (no action if False)IF salesTotal > 10000 THEN LET bonus = 1000END IF

The Selection structure is usually in the form of an IF-THEN-ELSE statement. For example, IF purchasePrice > 1000 THEN LET discountRate = 0.10, ELSE LET discountRate = 0.05, END IF. A variation is the IF-THEN statement, which does not include an ELSE, because no action is taken if the condition is false. IF salesTotal > 10000 THEN LET bonus = 1000, END IF.5Repetition Structure(Page 312)Comprised of a LOOP statementMay contain a sequence within the loopExample:DO WHILE count < 5 INPUT n LET sum = sum + n LET count = count + 1LOOPAnother kind of loop example:DO INPUT n LET sum = sum + n LET count = count + 1LOOP UNTIL count = 5Make sure the loop eventually terminates!

The Repetition structure is made up of a LOOP statement of some kind. Remember, there are 2 pre-test loops (DO WHILE-LOOP, DO UNTIL-LOOP), and two post-test loops (DO-LOOP WHILE and DO-LOOP UNTIL). The statements within a loop may be in sequence. The first example is a pre-test loop, because the condition is evaluated first. The next one, a post-test loop, executes the statements first, and then evaluates the condition. It is VERY IMPORTANT to make sure that something happens inside the loop so that it eventually terminates. Otherwise, your program is stuck in what is called an infinite loop, and you have to kill the program at the operating system level, which is not a good idea.6Beyond Structured Programming (Page 312)Traditional design:Programs are centered around proceduresInstructions define how data are used and storedIf data structure changes, programs must changeObject-oriented programming (OOP)Programs are centered around objectsObjects contain data and proceduresPrograms direct the interaction between objectsGraphical user interfacesUser determines order of events, not programOOPs go well with GUIs

It is good to get a firm basis in programming learning traditional design, which the flowcharts and pseudocode in this book emphasize. Programs are centered around the procedures or actions of the program. Instructions define how the data are stored and used. A downside of this is that if the data structure changes, all the programs that use the data must be changed. In the 1980s, a new model of programming was developed, called Object-Oriented Programming, or OOP. In this model, programs are centered around objects, which are just about anything you can describe. Employees, animals, cars, printers, concerts and schedules are all objects. They are designed to contain data about themselves, and also built-in procedures for how to interact with other objects. For example, a TV object might have a procedure called PowerOn. Programs direct the interaction between objects, using their data and their procedures. Graphical user interfaces (GUIs) also became popular in the 1980s, where instead of the programmer determining the order of events, the user determines them, by clicking on buttons and interacting with forms. Object-oriented programming goes very well with graphical user interfaces.7Section 9-4: Problem Solving with Structured Design (Page 314)Program development is problem solvingDefine the problem (identify components)Outline the solution (IPO chart)Design an algorithm (pseudocode/flowchart)Desk check the algorithmCode and document the programRun the program on a computer (fix errors)Maintain the program (add features, upgrade)

Way back in Chapter 1, we discussed that this book is about problem solving. So is the development of a program. In order to create a correct, efficient and successful program, you need to follow these problem-solving steps. Define the problem by identifying the parts or components of it. Outline the solution, and you can use an IPO chart for that. Design an algorithm to specify the exact steps, using pseudocode or a flowchart. Desk check the algorithm (you are telling the computer what to do). Then, with a real programming language, code and document the program, run it on a computer and fix the mistakes, and finally, maintain the program if necessary by adding features and upgrading it to keep up with new developments.8Modular Design (Page 315)Top-down design encourages that large programs be separated into distinct modules, each with its own taskExample: Payroll program modulesGet data (hourlyWage, hoursWorked, deductions)Calculate (grossPay, netPay, ytdUpdate)Pay employees (print check, directDeposit, etc.)Textbook emphasizes data processing modulesInitial (initialize variables, print headers, etc.)Detail (input and calculations for each record, etc.)Totals (print totals, update files, etc.)

Top-down design is a methodology where large programs can be separated into distinct modules, each with its own task. For example, if you are creating a payroll program, you might separate it into separate modules for getting the data you need, performing the calculations, and distributing the pay to the employees. The textbook emphasizes data processing, which often falls into three modules: the initial module (where you initialize variables, print headers, and otherwise get ready for the record processing), a detail module (where you process each record and do necessary calculations) and a totals module (where you finish up by printing totals, updating files, and other final operations).9Flowcharts for Modular Programming (Page 318)New symbol: predefined processLike process symbol, with stripes on the sideUsed to identify a moduleThe main program (which is a module) calls another module (called a submodule)Execution transfers to that submoduleSubmodule flowchart sectionTerminators are Start and ReturnSubmodules can call other submodulesExecution returns to calling module when done

When you create a separate module to do a task, there is a new flowchart symbol to use. It is called a predefined process, and so it looks like the process symbol (the rectangle), but it has stripes on the side. It has the name of the module in it. The main program calls a submodule, and at that point, the program execution transfers to that submodule. In the submodule flowchart section, the terminator symbols are labeled Start and Return, instead of Start and Stop for the main program. Submodules can call other submodules, too. Whenever a submodule is finished, execution returns to the place from where it was called.10Structure Charts(Back to Page 315)Structure charts show which modules call which other modulesDo not confuse them with flowchartsThey do not show logic (IF, LOOP, decisions, etc.)Useful when adding modules, renaming: documentation

We skipped ahead a little bit, to talk about what modules are. Now we go back to page 315, so that we can understand structure charts, which are just diagrams to show which modules call which other modules. Do not confuse structure charts with flowcharts: they do not show logic. They dont have IF statements, LOOPs, decisions or any logic. They are just a useful tool for documentation, especially when you need to add modules, rename them, or otherwise alter the structure of your program.11Pseudocode for Modular Programming (Page 317)Pseudocode uses the word CALLSeparate pseudocode sections for submodulesLike flowcharts, execution transfers to submodule until complete, then returnsMany people use Start and Stop statements in main modules, Start and Return statements in submodulesDo practice problems on page 321

Modules can be used in pseudocode, too. In fact, any pseudocode can be represented in a flowchart, and any flowchart can be represented in pseudocode. Pseudocode uses the word CALL to execute a submodule. Each submodule has its own separate section. Like in flowcharts, execution transfers to the submodule until it is complete, and then control returns to the place from where it was called. Many people use the flowchart terminator terms START and STOP as pseudocode statements, and START and RETURN statements in pseudocode for submodules. Do the practice problems on page 321.12Section 9-5:Study Materials (Page 322)Chapter summaryAlgorithm design toolsStructured English, pseudocodeIPO chartsFlowchartsStructure chartsProgram design conceptsTop-down developmentModular designStructure theorem (sequence, selection, repetition)Review questionsProblem solutions (more detail in Student Solutions Manual)

Be sure you are familiar with these terms and what they mean: the algorithm design tools of Structured English and pseudocode, IPO charts, flowcharts and structure charts. Know the program design concepts of top-down development, modular design, and what the structure theorem is and the three control structures. Be able to answer the review questions, and use the Student Solutions Manual to check the answers to the even-numbered practice problems.13CINS 113Computer LOGICChapter 9: Structured Program DesignSections 9-3 thru 9-5The EndThank You!

This is the end of this presentation on Chapter 9, Structured Program Design, Sections 9-3 thru 9-5. Thank you!14