Chapter12 - Flow Charts
Chapter12 - Flow Charts
Chapter 12
FLOWCHARTS
2
Problem/question:
I would like to write a program where this program will calculate
and show me the result for the summation of 2 numbers
(computer program a set of instructions
The program will executed by the computer. You know we
(programmer use English in coding while computer can only
understand machine/binary language)
Programmer translator computer
English in coding translate…. Binary language
(this process is taking but software development tool/application
Programmer use different programming language, C, C++, Java,
python)
3
Problem/question:
I would like to write a program where this program will calculate
and show me the result for the summation of 2 numbers.
Solution ??? (how to write the instructions – step by step)
Before write the program/codes
We need to understand the problem/question analysis/study
sum=num1+num2
Process assignment statement/
Formula statement
e.g. x=45
sum=num1+num2
Display “Enter first
Number: “
Input and output
display statement
num2
Enter num1
Enter num2
7
Problem 2
Draw a flowchart for a program where the program will
calculate and display the selling amount based on the unit
price and the quantity sold of the product enter by the user.
(use variable concept to store value)
Solution in flowchart
start
Enter unitPrice
Result:
Display “Enter
Quantity sold ”
Enter unit Price 3.50
Enter Quantity sold 4
Enter quantitySold
Selling amount RM 14.00
Sellingamt=unitPrice *
quantitysold
End
9
Operators use
Mathematic operators (to form formula statement for
calculation purpose)
* (multiply), / (divide), +, -, mod (modulus – divide but
take remainder as the result)
2x3 (incorrect), 2*3 (correct), 4/2
5 / 2 = 2.5, 5 mod 2 = 1
Result = 5 mod 2 end up the value store in the result
variable is 1
Display “Result is”, result Result is 1 (see this on
screen)
Display result 1 (see this on screen)
Sequence of this operator
( ), Mod, /, *, + , -
10
Operators used
Relational operators (will use to form a condition expression)
Condition expression will return True/False as result (logical
result – T/F)
> (more than)
E.g. 4 > 7 (false), 5>2 (true)
>= (more than and equal) ≥ (incorrect)
E.g. 4>=4 (true), 14>=2 (true)
< (less than)
E.g. 4<7 (true), 5<2 (false)
<= (less than and equal) ≤ (incorrect)
E.g. 4<=4 (true), 14<=2 (false)
<> (not equal to)
E.g. 4 <> 4 (false), 5 <> 2 (true)
== (equal to)
E.g. 4==4 (true), 5==2 (false)
Operator used 11
SELECTION STRUCTURE
METHODS USED IN SELECTION STRUCTURE
M1 – single if-then statement (without the else)
M2 – if-then-else statement (single)
M3 – multiple if-then statements (without else)
M4 – nested if-then-else statement
SELECTION STRUCTURE
14
M2 – if-then-else statement
condition
E.g if (condition<s>) then
action-statement-1
true false
else
action-statement-2
end-if
statement-x (consider outside the if-then-else)
SELECTION STRUCTURE
When will use it in the program? When the program involve
decision making.
Question: Draw a flowchart to ask user enter his/her mark, then
tell the user if he/she failed or passed (if mark >= 60 – pass,
any mark below 60 is fail)
Use IPO
Input enter a value (mark)
Enter mark (45, 67, 90, 23, 21, 80, 99)
Process
M1 – if (mark>=60) then display “Pass !!” (not appropriate)
M2 – if (mark>=60) then display “Pass !!” else display “Fail !!”
M3 – if (mark>=60) then display “Pass !!”
if (mark<60) then display “Fail !!”
Output a message indicate pass OR a message indicate
fail (can only be 1)
19
Start
START
Display “Enter mark: “ 1
Enter mark 2 Display “Enter
if (mark>=60) then 3 Mark “
Display
“The End”
END
20
Start START
Display “Enter mark: “ 1
Display “Enter
Enter mark 2 Mark “
if (mark>=60) then 3
Enter mark
Display “Pass !! “
endif
4
(mark>=60)
if (mark<60) then true
false
Display “Fail !!” Display
END
21
Questions
1. Draw a flowchart to show the logic of the solution for a problem where
the system will determine whether a number enter by the user is odd or
even. Display an appropriate message for the output.
2. Draw a flowchart to show the logic of the solution for the below
problem: Write a program to compute the electricity bill for the month,
where the user will enter the previous value of the meter reading and
current value of meter reading. If the number of used electricity is
below / equal to 1000 then the rate of the electricity is RM0.20 per unit.
If more than 1000, first 1000 units with the rate of RM0.20 and the
remainder units used with the rate of RM0.25.
Sample output: (for example)
Grading system
A+ (96% – 100%), A (90% – 95%), A- (86% – 89%), B+ (82% – 85%), B
(78% – 81%), B- (74% – 77%), C+ (70% – 73%), C (65% – 69%), C- (60%
– 64%), D+ (55% – 59%), D (50% – 54%), F (Below 0-49%)
END
24
ADDITIONAL QUESTIONS
Draw a flowchart for a program that will receive two
distinct positive integers as input and will print the
difference of the two numbers. Be sure that your
program will print 6 either if 9 (the first number) and
15 (as the second number) is enter or 15 (the first
number) and 9 as the second number is enter.
Additional question
Draw a flowchart for a program that will accept one student’s
name, IC No., and marks of three subjects. Calculate the average
marks for the student and the average marks will determine the
final grade of the student. The assessment will be as follow:
A: 70 –100
B: 60 –69
C: 50 –59
D: 40 – 49
F: 0 –39
The program should have the sample output as the following.
Name : Samuel Mann
IC. No.: 791017-10-5512
Grade : A
26
Begin
Display “Enter student name: “
Enter studName
Display “Enter student IC.: “
Enter studIC
Display “Enter 3 subjects’ marks: “ m1, m2,m3
x=1 start counting from 1
while (x<=3) the 3 is represent the 3x of entering values
Enter marks
total=total+marks accumulator : to accumulate the 3 values
x=x+1 counter to increase the value in the x
endwhile
average=total/3
if (average >=70 and average<=100) then
grade=A
else
if(average >=60 and average <=69) then
grade =B
.
.
27
Exercise : Looping
Draw a flowchart to show the logic of a program to calculate the total of all ODD integers
between 50 and 100 for the first 10 numbers entered.
using (1) while loop and (2) do-while loop.
Begin
Display “Enter 10 numbers: “
x=1
Total=0 //start value
while (x<=10)
Enter number
If (number >50 and number <100 and number mod 2=1) then false
total=total+number X<=10
Endif
X=x+1
true
Endwhile
Display “Total is “, total
End
false
true if
X=x+1
28
Exercise : Looping
Draw a flowchart to show the logic of a program to calculate the total of all ODD integers between
50 and 100 for the first 10 numbers entered.
using (1) while loop and (2) do-while loop.
Begin
Display “Enter 10 numbers: “
x=1
Total=0 //start value
Do X=1, total=0
Enter number
If number >50 and number <100 and number mod 2=1 then
Enter number
total=total+number
Endif
X=x+1
while (x<=10) true if
Display “Total is “, total false
End
2+3*4
(2+3)*4
X=x+1
true false
X<=10
29
LOOPING STRUCTURE
- What is it? Will repeatedly execute certain statements
When to use looping structure in the program?
-when we want to execute certain statements in a number
of times
For example:
A program will calculate the total of 3 enter numbers
Input – user will need to enter 3 numbers (each number
need to store in a location/variable) x, y, z (variables)
Enter x, y, z
Process – calculate total=x+y+z
Output – display total
30
techniques/methods
To state starting value, StartV=0
Know how many times to loop 5x
To state the end value to have condition
(StartV<=EndV)
In order to count use statement to do counting
Counter statement x=x+1 (x is a counter variable)
StartV EndV
0 4 (0,1,2,3,4)
1 5 (1,2,3,4,5)
2 6 (2,3,4,5,6)
33
2) While loop
Format
<statement to state the start value> - not loop
while (condition)
<statements to loop>
<counter statement>
endwhile <end of the loop>
<statement here – mean execute one time>
Logic
the statements inside the loop body will execute as long
as the condition is true; the loop will stop once the
condition is false
35
3) Do-while loop
Format:
<statement – will execute only one time>
Do
<loop-body: any statements for loop>
while (condition)
<statement – will execute only one time>
Logic
The statements in the loop body will execute as long as
the condition is true; the loop will stop once the condition
is false
37
Looping type 1
Begin Begin
Display “Enter 5 numbers:”
Display “Enter 5 numbers: “
x=1
while (x<=5)
x=1
Enter number do
total=total+number Enter number
x=x+1 total=total+number
Endwhile x=x+1
Display “Total is “, total While (x<=5)
End Display “Total is “, total
What you will see on screen
End
Enter 5 numbers:
12 34 45 56 78
Total is 225
38
Example: Start
Begin
Display “Enter
Display “Enter 5 numbers: “ 5 numbers: “
For (x=1, x<=5, x=x+1)
Enter number 1
x=1
5 false
total=total+number x=x+1
Endfor
Display “Total is “, total Enter Number
End
true
Total=total
+number
Display “Total is “,
total
End
39
Display “Total is “,
total
End
40
End
41
Problem Solving
Can be solved in a series of actions
Problem solving steps:
1. Identify the problem
2. Understand the problem
3. Identify alternatives (solutions)
4. Select the best solution
5. Prepare a list of steps (instruction)
6. Evaluate the solution
42
SOLUTIONS
Algorithmic Heuristic
the best alternative cannot be reached through
solution reached by direct set of steps
completing actions in – require reasoning built on
steps – algorithm knowledge & experience
– a process of trial & error
43
Program
set of instructions that
make up the solution
after they have been
coded into a particular
computer language
T F
i < 10
Display “i is
less than 10”
Exit
48
Display Area
Exit
Writing the Algorithms 50
Algorithm …
1. Constant PI = 3.142 PI = 3.142
2. Variable Radius = 10
3. Variable Area = 0.0 …
4. Area = PI x Radius x Radius
5. Display Area …
Radius = 40
Output at monitor screen
…
314.2 Area = 314.2
0.0
…
Drawing the Flowchart & Writing Algorithm 51
Exit
52
PROGRAM STRUCTURE
53
Program Structure
Use FOUR logic structures to ensure the solution flows
smoothly from one instruction to the next, rather than jumping
between points
1. Sequential structure executes instructions one after another in a
sequence
2. Decision structure branches to execute one of the possible sets of
instructions
3. Loop structure executes a set of instructions many times
4. 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
Figure 1: Sequential Logic Structure
54
Figure 2: Decision Logic Structure
55
Figure 3: Loop Logic Structure
56
57