program logic formulation - ohio state university

27
Lect 3 P. 1 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition Structured Engineering Problem Solving and Logic Diagrams Lecture 3

Upload: reggie-niccolo-santos

Post on 26-May-2015

279 views

Category:

Software


4 download

DESCRIPTION

* Tools for Program Development * Top-down Stepwise Refinement * Some Types of Logic Diagrams * Characteristics of Flow Charts * Flow Charting Symbols

TRANSCRIPT

Page 1: Program Logic Formulation - Ohio State University

Lect 3 P. 1

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Structured Engineering Problem Solving and Logic Diagrams

Lecture 3

Page 2: Program Logic Formulation - Ohio State University

Lect 3 P. 2

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

TOOLS FOR PROGRAM DEVELOPMENT

• A variety of tools and techniques can be used in the process of program development

• Useful for organizing the tasks in problem solving

• Many of the tools are focused on the:

– development or formulation of algorithms

– representation of algorithms

– refinement or structuring of algorithms

Page 3: Program Logic Formulation - Ohio State University

Lect 3 P. 3

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

TOOLS FOR PROGRAM DEVELOPMENT

• Top-down design technique – Start with overall function and perform several

step-wise refinements• Pseudo code– Artificial and informal language that helps

programmers develop algorithms• Logic diagrams– Alternate representations of algorithms

including graphic and state representations

Page 4: Program Logic Formulation - Ohio State University

Lect 3 P. 4

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

TOP-DOWN STEPWISE REFINEMENT

• Begin with a single statement that conveys the overall function of the program. This is a complete (but simple) representation of the program.

• Divide this "top" statement into a series of smaller tasks and list them in the order in which they must be performed to produce a first refinement.

Page 5: Program Logic Formulation - Ohio State University

Lect 3 P. 5

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

TOP-DOWN STEPWISE REFINEMENT

• Next, refine each of the smaller tasks into yet smaller steps, defining specific "variables" as may be needed in this second refinement.

• Continue refinement until algorithm is fully developed.

• When combined with pseudo code, writing the program is normally straightforward.

Page 6: Program Logic Formulation - Ohio State University

Lect 3 P. 6

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example

• The Problem:

The students in a class have taken their first quiz. The grades (in the range of 0 to 100) are available. Determine the class average on the quiz.

• Givens:

Grades, Possible range of legitimate grades.

Page 7: Program Logic Formulation - Ohio State University

Lect 3 P. 7

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

• Unknowns:

How many grades to be averaged?

• Assumptions:

A "grade" not in the range of expected grades could be used to indicate the that all of the legitimate grades have been entered. (Called "sentinel" or "flag".)

Page 8: Program Logic Formulation - Ohio State University

Lect 3 P. 8

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

• Using pseudo code, we begin by representing the top:

– Determine the class average for the quiz

Page 9: Program Logic Formulation - Ohio State University

Lect 3 P. 9

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

• Next, perform the first refinement:

– Determine the class average for the quiz• Initialize variables• Input, sum, and count quiz grades• Calculate and print the class average

Page 10: Program Logic Formulation - Ohio State University

Lect 3 P. 10

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

• Next, refine each one of these smaller tasks:

– Initialize variables• Initialize a running total to zero• Initialize a grade counter to zero

Page 11: Program Logic Formulation - Ohio State University

Lect 3 P. 11

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

Input, sum, and count quiz grades

Input the first grade

While the user has not entered the "flag"

Add this grade into the running total

Add one to the grade counter

Input next grade (possibly the "flag")

Page 12: Program Logic Formulation - Ohio State University

Lect 3 P. 12

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Top-Down Example (continued)

Calculate and print the class average

If the grade counter is not zero

Set the class average to the running

total divided by the grade counter

Print the average

Else

Print "No grades were entered"

Page 13: Program Logic Formulation - Ohio State University

Lect 3 P. 13

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Handling Special Cases

• Notice that the top-down refinement example included the handling of a "special case".

• Many of you are aware of the special treatment that Windows gives to the extreme "special cases" (sometimes called the BSOD or Blue Screen of Death…)

Page 14: Program Logic Formulation - Ohio State University

Lect 3 P. 14

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Handling Special Cases

Page 15: Program Logic Formulation - Ohio State University

Lect 3 P. 15

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Some Types of Logic Diagrams

• Flow Charts -- graphic representation of an algorithm– an aid to writing the program– no formal standards, but common guidelines

• Action Diagrams -- technique for diagramming of control structures of a program– an outline of the computer application– what things happen, when, where, how many

times

Page 16: Program Logic Formulation - Ohio State University

Lect 3 P. 16

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Characteristics of Flow Charts

• Useful tool in program development• Not a complete description of program• Not only tool to use• Made before writing the program• Program might differ from flowchart• Only executable statements are shown• Specific equations and tests not included• Every main and sub-program is charted

Page 17: Program Logic Formulation - Ohio State University

Lect 3 P. 17

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

• The commonly used flowcharting symbols follow

• Refer to Section 9 of the H192 "Class Notes" for a more complete description of the various flowcharting symbols normally used

Page 18: Program Logic Formulation - Ohio State University

Lect 3 P. 18

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting SymbolsBegin or End a Procedure:

"Main Program" "Subprogram"

Begin

End

START

BEGIN

STOP

END

ENTER

RETURN

Page 19: Program Logic Formulation - Ohio State University

Lect 3 P. 19

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

Write to Screen:

Read from Keyboard:

INFORM MSG

ERROR MSG

RESULTS

INPUTPROMPT

INPUT

INPUTPROMPT

OPTION

Page 20: Program Logic Formulation - Ohio State University

Lect 3 P. 20

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting SymbolsRead from a File:

Read from a File with a Check for End-of-File:

READ

EOF? Y

N

READ

EOF? Y

N

READ

WRITE

Page 21: Program Logic Formulation - Ohio State University

Lect 3 P. 21

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

Decision or Selection Structure:

General Processing:

QUIT? Y

N

A<B? F

T

process

Page 22: Program Logic Formulation - Ohio State University

Lect 3 P. 22

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

Definite Loop:

Indefinite Loop:

y

n init

inc <= LIM?

do while index <= limit

y

n

Page 23: Program Logic Formulation - Ohio State University

Lect 3 P. 23

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

On-Page Connectors:

Off-Page Connectors:

1 2 2

1 From To

To From

A

A

Page 24: Program Logic Formulation - Ohio State University

Lect 3 P. 24

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Flow Charting Symbols

Call (or Invoke) a Subprogram:

subname

Page 25: Program Logic Formulation - Ohio State University

Lect 3 P. 25

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Daily Assignment F2

• Problem:

There are a number of apples in a large box which must be sorted into baskets. An apple is either a "large red" one, a "small red" one, or a "green" one.

• Develop a flow chart or algorithm to solve the problem

Page 26: Program Logic Formulation - Ohio State University

Lect 3 P. 26

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Daily Assignment F2

Assumptions:1. There are 3 empty baskets present and they are

labeled:#1 – large red, #2 – small red, #3 – green

2. There are counters of some sort present.3. The person sorting is not color blind and can tell

the difference between large and small apples.4. Only these three kinds of apples are in the large

box.

Page 27: Program Logic Formulation - Ohio State University

Lect 3 P. 27

Engineering H192 - Computer Programming

Winter Quarter The Ohio State UniversityGateway Engineering Education Coalition

Daily Assignment F2

• Are there any unknowns?

• Start with the overall problem

• Develop the steps to solve the problem