1 Chapter 12 FLOWCHARTS
1
Chapter 12FLOWCHARTS
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
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
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
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
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
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
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
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
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
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
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
13
PROGRAM STRUCTUREPROGRAM STRUCTUREPROGRAM STRUCTUREPROGRAM STRUCTURE
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
15Figure 1: Sequential Logic Structure
16Figure 2:Decision Logic Structure
17Figure 3:Loop Logic Structure
18
Figure 4: Case Logic Structure
19
Problem Solving with Computers (e.g.)
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