0% found this document useful (0 votes)
118 views

Chapter12 - Flow Charts

The document discusses flowcharts and selection structures in programming. It provides examples of how to represent input, process, and output steps using flowchart symbols. It also explains different selection structure methods like single if-then statements, if-then-else statements, multiple if-then statements, and nested if-then-else statements. Operators for mathematical calculations, relational comparisons, and logical expressions are also defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Chapter12 - Flow Charts

The document discusses flowcharts and selection structures in programming. It provides examples of how to represent input, process, and output steps using flowchart symbols. It also explains different selection structure methods like single if-then statements, if-then-else statements, multiple if-then statements, and nested if-then-else statements. Operators for mathematical calculations, relational comparisons, and logical expressions are also defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 59

1

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

Tools can use to analyze the problem


1)IPO chart – input (data) process (formula?) output
(result/information)
2)Flowchart – to show the solution for the problem in a logical way.
Use different shapes/symbols to represent input, process, output
Problem/question: 4

I would like to write a program where this program will calculate


and show me the result for the summation of any 2 numbers. (how
to decide which 2 numbers??? U want the user to enter 2 numbers)
Input  2 numbers
M1) in the program  fix the 2 numbers, 4 and 5
*M2) <user enter the 2 values> Enter/Input
Enter num1
Enter num2
Process  calculate the sum of 2 numbers
M1) sum=4+5
*M2) sum=num1 + num2
Output  the sum <to output  display
*M1) display sum
In a program, we use a concept call variable – store value? (is label
<has a name> in the program that represent the name of location
that store value)
5
Write them in proper step-by-step instructions /algorithm
Executing in sequential manner : sequential structure
program
Begin/Start
Display “Enter first number: “
Enter num1
Display “Enter second number: “
Enter num2
Sum=num1+num2
Display “The result of the sum is “
Display sum
End

To enhance the program!!


6

Flowchart shapes – sequential


Start or end

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)

1)Use IPO to analyze the question


input  2 values – number (unit price, quantity)
Enter unitprice
Enter quantity
process  sellingamt=unitprice * quantity
output  display sellingamt
1)Algorithm (optional)
2)flowchart
8

Solution in flowchart
start

Display “Enter unit


Price ”

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

Display “Selling amount


RM ”, sellingamt

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

 Logical operators (is use to join/combine more than 2


conditions together to form logical expression)
 Logical expression will return True or false as result
 E.g. (4>2 ?? 5<>2)  what is I want to combine these
2 conditions to make it as A condition
 NOT
 E.g NOT (5<>2)  Not True  False (final result)
 E.g NOT (4<3)  Not False  True (final result)
 AND  (T AND T AND F), as long as one condition is
false, then the result will be false
C1 C2 C3 RESULT
T T T T
T T F F
T F T F
F F T F
F F F F
12

 OR (T OR T OR T), as long as one of the condition is


true, then the result will be true
C1 C2 C3 RESULT
T T T T
T T F T
T F T T
F F T T
F F F F

 Need to follow the sequence


 NOT X=T AND F OR F AND NOT T x?
 AND X= T AND F OR F AND F
X= F OR F
 OR X = FALSE
13

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

 M1 – single if-then statement


condition
 E.g if (condition<s>) then
action-statement-1 (s)
true false
action-statement-2
end-if
statement-x (consider outside the if statement)

 Logic is action-statement will execute if the condition is


true, no action statement will execute if the condition is
false
 The action statement can be input (Enter x), display
(Display x), or even formula statement (x=a+b)
SELECTION STRUCTURE
15

 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)

 Logic is action-statement-1 will execute if the condition


is true, action statement-2 will execute if the condition is
false
 The action statement can be input (Enter x), display
(Display x), or even formula statement (x=a+b)
SELECTION STRUCTURE
16

 M3 – multiple if-then statement


condition
 E.g if (condition1) then
action-statement-1
true false
endif
if (condition2) then
action-statement-2
end-if
statement-x (consider outside the if statements)
 Logic is action-statement-1 will execute if the condition1 is true, no
action statement will execute if the condition1 is false
 Logic is action-statement-2 will execute if the condition2 is true, no
action statement will execute if the condition2 is false
 The action statement can be input (Enter x), display
(Display x), or even formula statement (x=a+b)
SELECTION STRUCTURE
17

 M4 – nested if-then-else statement


 E.g if (condition1) then
action-statement-1 condition1
else true false
if (condition2) then
action-statement-2 condition2
else true false
if (condition3) then
action-statement-3 condition3
else true false
if (condition4) then
action-statement-4 condition4
else true false
action-statement-5
endif
endif
endif
end-if
statement-x (consider outside the nested if statements)
18

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 “Pass !! “ T Enter mark


else
Display “Fail !!” F
(mark>=60)
Endif
Display “The End” 4 true false

End Display Display


“Pass !!” “Fail !!”

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

Endif 5 “Pass !!”

Display “The End”


End (mark<60)
true false
Display
“Fail !!”

Display “The End”

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)

Enter Previous Meter Reading : 12500


Enter Current Meter Reading : : 13000
No. of Used Electricity : 500

The charge is RM____?_______


22

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%)

 Solution :1) Multiple if-then statements 2) nested if-then-else


 Enter mark
If (mark >=0 and mark <=49) then Display “Grade F”
else
If(mark>=50 and mark <=54) then display “Grade D”
else
If (mark >=55 and mark<=59) then display “Grade D+”
else
…….
else
If (mark >=96 and mark <=100) then display “Grade A+”
23

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.

 Draw a flowchart a program that will receive two


integers as input and then will print the message
“Same Signs” if both of the integers is positive or both
is negative. Print the message “Opposite signs” if one
of the integers is positive and the other one is
negative.
25

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

Loop x500 (to sum 5 numbers)


Number  variable to store the value
Total Number Assignment statement (read from
0 34 right to left)
0+34=34 12 x=4
Total=a+b
34+12=46 4
Sum=2+3
46+4=50 34 There is statement call accumulator
50+34=84 1 (to calculate the total of n numbers)
84+1=85 Total=0
Total = Total + number
Display total  85
Display number 1
31

There are 2 types of looping


1) The number of times of looping is fix in the program
because the programmer would like the program to
execute based on a fix number of time
for example, to total up 10 numbers
login – give you 3 chances

2) The number of times of executing statements is


unknown by the programmer – is going to determine
by the user (meaning the user need to provide
instruction to the program either to continue or to
stop)
For example:
Do you want to continue for next transaction? no
In looping structure, you can use 3 32

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

1) For loop Example:


Begin
Format Display “Enter 5 numbers: “
Begin For (x=1, x<=5, x=x+1)
<statement> no loop only do 1 time Enter number
for (startvalue, condition, counter) total=total+number
<statements for looping> Endfor
Endfor Display “Total is “, total
<statement> no loop End
End Example:
X x>=1 Enter number Begin
5 5>=1(T) 5 Display “Enter 5 numbers: “
4 4>=1(T) 9 For (x=5, x>=1, x=x-1)
3 3>=1 (T) 12 Enter number
2 2>=1(T) 11
total=total+number
1 1>=1(T) 10
0 0>=0 (F) – loop will stop
Endfor
Display “Total is “, total
34

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

Looping type 1 – know the number


Begin
Display “Enter 5 numbers:”
x=1
while (x<=5)
Enter number
if (number %2=0)
total=total+number
Endif
x=x+1
Endwhile
Display “Total is “, total
End
Q
Is to ask the user to enter 5 numbers and
calculate the total for the even numbers only
36

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

Flowchart for While loop Begin


Begin Display “Enter
Display “Enter 5 numbers:” 5 numbers: “
x=1
total=0 X=1
while (x<=5) Total=0
Enter number
total=total+number false
(x<=5)
x=x+1
Endwhile true
Display “Total is “, total Enter Number
End
What you will see on screen
Total=total+number
Enter 5 numbers:
12 34 45 56 78
Total is 225 X=x+1

Display “Total is “,
total

End
40

Flowchart for do-While loop


Begin
Begin Display “Enter
5 numbers: “
Display “Enter 5 numbers: “
x=1
X=1
total=0 Total=0
do
Enter number Enter Number
total=total+number
x=x+1 Total=total+number
While (x<=5)
X=x+1
Display “Total is “, total
End true false
(x<=5)
X
6 Display “Total is “,
total

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

Problem Solving with Computers


Solution Result
instructions listed during The outcome
step 5 of problem solving or completed
– must be followed to computer-assisted
produce best results answer

Program
set of instructions that
make up the solution
after they have been
coded into a particular
computer language

Computers deal with algorithmic solutions & heuristic


solutions (artificial intelligence)
44

Constants & Variables


Constant
An alphabetical and/or
Variable
numerical value that (identifiers) May
never changes during the change during
processing of all the processing
instructions in a solution

Given a name & a location


in memory
- Referred to by given
name
45

Drawing the Flowcharts


 Connect blocks by exiting from one
and entering another
 The arrowhead is necessary
FLOWLINES

Start  Indicate the start & end of a


program
Exit  Start has one flowline exiting
 Exit has one flowline entering
46

Drawing the Flowcharts (cont.)


 For calculations,…
 Has one flowline entering and one
exiting
PROCESSING

 Indicates input to and output from


the computer memory
 Has one entrance & one exit
I/O
47

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
Decision action when the resultant is FALSE

T F
i < 10
Display “i is
less than 10”
Exit
48

Problem Solving with Computers

Problem: Display Area of a Circle

Steps to solve the problem:


1. Declare & Initialize PI value
2. Declare & Initialize Radius value
3. Declare & Initialize Area value
4. Calculate Area
5. Display the value of Area
Drawing the Flowchart & Writing Algorithm 49

Display Area of a Circle


Start

Declare & initialize


PI, Radius and Area
Algorithm
1. Constant PI = 3.142
2. Variable Radius = 10
Calculate Area 3. Variable Area = 0.0
4. Area = PI x Radius x Radius
5. Display Area

Display Area

Exit
Writing the Algorithms 50

Display Area of a Circle

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

Display Area of a Circle


Start

Declare & initialize


PI, Radius and Area
Algorithm
Get Radius 1. Declare Constant PI=3.142,
2. Variable Radius=0
Calculate Area 3. Variable Area=0.0
4. Get Radius
5. Area = PI x Radius x Radius
Display Area 6. Display Area

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

Figure 4: Case Logic Structure


58

Problem Solving with Computers (e.g.)


59

Draw a flowchart to calculate bonus for an employee


based on the following table:

Annual Hours Worked Bonus

Less than 2000 hours RM 500

Between 2000 and 2499 hours RM 500 + 1 month salary

More than 2500 hours RM500 + 2 months salary

You might also like