CSC1100 - Chapter12 - Flow Charts

20
1 Chapter 12 FLOWCHARTS

description

ဖြစ်စဉ်စီးကြောင်းပြဇယားများ

Transcript of CSC1100 - Chapter12 - Flow Charts

Page 1: CSC1100 - Chapter12 - Flow Charts

1

Chapter 12FLOWCHARTS

Page 2: CSC1100 - Chapter12 - Flow Charts

2

Problem Solving� Can be solved in a series of actions � Problem solving steps:

1. Identify the problem2. Understand the problem3. Identify alternatives (solutions)4. Select the best solution4. Select the best solution5. Prepare a list of steps (instruction)6. Evaluate the solution

Page 3: CSC1100 - Chapter12 - Flow Charts

3

SOLUTIONS

Algorithmic Heuristic

the best alternative solution reached by completing actions in steps – algorithm

cannot be reached through direct set of steps

– require reasoning built on knowledge & experience

– a process of trial & error

Page 4: CSC1100 - Chapter12 - Flow Charts

4

Problem Solving with Computers

Solutioninstructions listed during step 5 of problem solving

– must be followed to produce best results

ResultThe outcome or completed

computer-assisted answer

Program

Computers deal with algorithmic solutions & heuristic solutions (artificial intelligence)

Programset of instructions that make up the solution after they have been

coded into a particular computer language

Page 5: CSC1100 - Chapter12 - Flow Charts

5

Constants & Variables

ConstantAn alphabetical and/or numerical value that

never changes during the processing of all the

instructions in a solution

Variable(identifiers) May

change during processing

Given a name & a location in memory

- Referred to by given name

Page 6: CSC1100 - Chapter12 - Flow Charts

6

Drawing the Flowcharts

� Connect blocks by exiting from one and entering another

� The arrowhead is necessary

FLOWLINESFLOWLINES

� Indicate the start & end of a program

�� StartStarthas one flowline exiting�� ExitExit has one flowline entering

StartStart

ExitExit

Page 7: CSC1100 - Chapter12 - Flow Charts

7

Drawing the Flowcharts (cont.)

� For calculations,…� Has one flowline entering and one

exiting

PROCESSINGPROCESSING

� Indicates input to and output from the computer memory

� Has one entrance & one exitI/OI/O

Page 8: CSC1100 - Chapter12 - Flow Charts

8

Drawing the Flowcharts (cont.)

� Indicates a decision� Has one entrance & two exits� One exit is the action when the

resultant is TRUE & other exit is the action when the resultant is FALSEDecisionDecision

i < 10T F

Display “i is less than 10”

ExitExit

Page 9: CSC1100 - Chapter12 - Flow Charts

9

Problem Solving with Computers

Problem: Display Area of a Circle

Steps to solve the problem:1. Declare & Initialize PI value2. Declare & Initialize Radiusvalue3. Declare & Initialize Areavalue4. Calculate Area5. Display the value of Area

Page 10: CSC1100 - Chapter12 - Flow Charts

10Drawing the Flowchart & Writing AlgorithmDisplay Area of a Circle

Start

Declare & initializePI, Radius andArea

Algorithm1. Constant PI = 3.1422. Variable Radius = 10

Exit

Calculate Area

Display Area

2. Variable Radius = 103. Variable Area = 0.04. Area = PI x Radius x Radius5. Display Area

Page 11: CSC1100 - Chapter12 - Flow Charts

11Writing the Algorithms Display Area of a Circle

Algorithm1. Constant PI = 3.1422. Variable Radius = 103. Variable Area = 0.04. Area = PI x Radius x Radius5. Display Area

PI = 3.142

…5. Display Area …

Radius = 40

Area = 0.0

314.2314.2

Output at monitor screen

Page 12: CSC1100 - Chapter12 - Flow Charts

12Drawing the Flowchart & Writing AlgorithmDisplay Area of a Circle

Start

Declare & initialize PI, Radius andArea

Algorithm1. Declare Constant PI=3.142, 2. Variable Radius=0

Get Radius

Exit

Calculate Area

Display Area

2. Variable Radius=03. Variable Area=0.04. Get Radius5. Area = PI x Radius x Radius6. Display Area

Page 13: CSC1100 - Chapter12 - Flow Charts

13

PROGRAM STRUCTUREPROGRAM STRUCTUREPROGRAM STRUCTUREPROGRAM STRUCTURE

Page 14: CSC1100 - Chapter12 - Flow Charts

14

Program Structure� Use FOUR logic structures to ensure the solution flows

smoothly from one instruction to the next, rather than jumping between points1. Sequential structure executes instructions one after another in a

sequence2. Decision structure branches to execute one of the possible sets of

instructionsinstructions3. Loop structure executes a set of instructions many times4. Case structure executes a set of instructions out of several sets

� Efficient by providing smooth transformation of data into information besides eliminating the rewriting of identical processes

� Techniques to improve program readability: the 4 logic structure, proper naming of variables, internal documentation and proper indentation

Page 15: CSC1100 - Chapter12 - Flow Charts

15Figure 1: Sequential Logic Structure

Page 16: CSC1100 - Chapter12 - Flow Charts

16Figure 2:Decision Logic Structure

Page 17: CSC1100 - Chapter12 - Flow Charts

17Figure 3:Loop Logic Structure

Page 18: CSC1100 - Chapter12 - Flow Charts

18

Figure 4: Case Logic Structure

Page 19: CSC1100 - Chapter12 - Flow Charts

19

Problem Solving with Computers (e.g.)

Page 20: CSC1100 - Chapter12 - Flow Charts

20

Draw a flowchart to calculate bonus for an employee based on the following table:

Annual Hours Worked Bonus

Less than 2000 hours RM 500Less than 2000 hours RM 500

Between 2000 and 2499 hours RM 500 + 1 month salary

More than 2500 hours RM500 + 2 months salary