01 2 introduction to flowcharting
DESCRIPTION
TRANSCRIPT
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 1
Introduction To Flowcharting
byJumail Bin Taliba
Faculty of Computer Science & Information System
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 2
Today’s Topics
• Flowchart Symbols• Control Structures• Some examples
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 3
Flowchart:
Represents an algorithm in graphical symbols
Example: Algorithm for multiplying two numbers
Start
Stop
Get AGet B
Display theResult C
Calculate ResutC=A*B
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 4
Terminal: Used to indicates the start and end of a flowchart. Single flow line. Only one “Start” and “Stop” terminal for each program. The end terminal for function/subroutine must use “Return” instead of “Stop”.
Process: Used whenever data is being manipulated. One flow line enters and one flow line exits.
Input/Output: Used whenever data is entered (input) or displayed (output). One flow line enters and one flow line exits.
Decision: Used to represent operations in which there are two possible selections. One flow line enters and two flow lines (labeled as “Yes” and “No”) exit.
Function / Subroutine: Used to identify an operation in a separate flowchart segment (module). One flow line enters and one flow line exits.
On-page Connector: Used to connect remote flowchart portion on the same page. One flow line enters and one flow line exits.
Off-page Connector: Used to connect remote flowchart portion on different pages. One flow line enters and one flow line exits.
Comment: Used to add descriptions or clarification.
Flow line: Used to indicate the direction of flow of control.
Flowchart Symbols
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 5
Example:
Start
Stop
Read ARead B
Display theResult C
Calculate ResutC=A*B
Start Terminal. Program starts here
Stop TerminalProgram ends here
Input.Enter values for A and B
Process
Output
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 6
Comments or description
Start
Read N, M
No
Yes
Stop
N = The number of studentsM = The number of subjects
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 7
Connectors on the same page
Start
2
1
1 2
Stop
1- connection on the same flowchart portion
2- connection on the different flowchart portion
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 8
Connectors on a different page
Page 1 Page 2
Start
No
1
Yes 1
2
Stop
2
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 9
Function
Page 1
AVRG (result, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
Printresult
Page 2
AVRG ( result,n1, n2,n3)
Return
sum = n1+ n2+n3
result = sum/3
End terminal must be a “Return”
Start terminal for aFunction is different.Do not use “Start”
The detail of how the function works is put in another flowchart.
This is known as Function-Definition
At this point, we only focus on whatto do. How to do it, it comes later.
This part is known asFunction-Call
Body of a function is the same with normal flowchart
This flowchart calculates the average of three numbers
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 10
Related terms and concepts
Page 1
AVRG (result, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
Printresult
Page 2
AVRG ( R, a, b,c)
Return
sum = a+ b+c
R = sum/3
AVRG is the function name
Objects enclosed by ( ) – result, n1, n2, n3 - are called parameters
Parameters used in a function-callare called actual parameters
result, n1, n2, n3 are actual parameters
Parameters used in a function-definitionare called formal parameters
R, a, b, c are formal parameters
Each formal parameter represents an actual parameter according to its order:
R represents result,a represents n1,b represents n2,c represents n3
The name of an actual parameter may bedifferent from its formal parameter
This flowchart calculates the average of three numbers
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 11
Related terms and concepts (cont.)
Page 1
AVRG (result, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
PrintR
Page 2
AVRG ( R, a, b,c)
Return
sum = n1 + n2 + n3
R = sum/3
In a function-definition, you should only use formal parameters – R, a, b, c
You shouldn’t use actual parameters
This is wrong!n1, n2, n3 are actual parameters.Should use a, b, c instead.
This is wrong!R is an formal parameters.Should use result instead.
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 12
Page 1
AVRG (average1, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
Printresult
Readn4, n5 , n6
AVRG (average2, n4, n5, n6)
result = (average1 +average2) / 2
Related terms and concepts (cont.)
Page 2
AVRG ( R, a, b,c)
Return
sum = a+ b+c
R = sum/3
A function may be called more than once
At this time:R represents average1,a represents n1,b represents n2,c represents n3
When comes to this:R represents average2,a represents n4,b represents n5,c represents n6
This flowchart calculates the average of three numbers
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 13
Related terms and concepts (cont.)
A function parameter may act as:
• InputData of the function
• OutputThe result of the function
• Both
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 14
Page 1
AVRG (result, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
Printresult
Page 2
AVRG ( R, a, b,c)
Return
sum = a+ b+c
R = sum/3
Function call:
result is the output parameter. n1, n2, n3 are the input parameters.
Function definition:
R is the output parametera, b, c are input parameters
Related terms and concepts (cont.)
This flowchart calculates the average of three numbers
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 15
Related terms and concepts (cont.)
Page 1 Page 2
EXCHG( x, y)
Return
hold = x
x = y
y = hold
Start
Stop
Printp, q
EXCHG (p, q)
Readp, q
Function call:
p and q act as both input and output parameters.
Function definition:
x and y act as both input and output parameters
This flowchart exchanges or swaps the value of x and y each other
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 16
Page 2
AVRG ( R, a, b,c)
Return
sum = a+ b+c
R = sum/3
Related terms and concepts (cont.)
If there is only one output parameter, the flowchart may “RETURN” the result
Example: let take a look again at the function that calculates the average of three numbers.
Original function flowchart:
Page 2
AVRG ( a, b,c)
Return R
sum = a+ b+c
R = sum/3
Since it has only one output, the output is “RETURN”
The output parameter (R) is removed from the formal parameter list
and the result is return
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 17
Related terms and concepts (cont.)
Since the function flowchart has been modified, the way of the function to be called will also be changed
Original main flowchart: Modified main flowchart:
Page 1
AVRG (result, n1, n2,n3)
Start
Stop
Readn1, n2 , n3
Printresult
Page 1
result =AVRG (n1, n2, n3)
Start
Stop
Readn1, n2 , n3
Printresult
Now, result is notanymore a parameterof the function-call
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 18
Control Structure
• Describe the flow of execution.• In flowcharts, flow of execution is represented by the arrow line.
• Types of control structure:•Sequential•Selection•Repetition
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 19
Sequential Structure
statement
statement
statement
Multiple statements considered as one statement
A statement means a command or an instruction
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 20
Selection Structure
If
(one-choice)
condition
statement
°
TRUE
FALSE statement
If set condition is true, execute the statement, else do nothing
“do it or don’t”
condition
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 21
Selection Structure (cont..)If-else
(two-choices)
condition
Statement 2Statement 1
°
statement
If set condition is true, execute the first statement, else execute second statement
TRUE FALSE
“do this one or the other one”
condition
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 22
Considered as one
statement
Nested if (if within if)
Selection Structure (cont..)
test1
test2
statement
°
°FALSE
FALSE
TRUE
TRUE
test1
°
°
FALSE
TRUE
it is an “one-choice” if
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 23
Complex if-else & if Statements
°
x
condition
statement
condition
statement
°
statement TRUE
TRUE
FALSE
FALSE
Considered as one statement
Selection Structure (cont..)
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 24
Repetition Structure
while Loop
It is a pre-test loop
statement
condition
body of loop
While a set condition is true, repeat statement (body of loop)
TRUE
FALSE°
condition
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 25
Repetition Structure (cont…)do-while Loop
It is a post-test loop
statement
FALSETRUE
Do the statement (body of loop) while a condition is true
statement
°
condition
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 26
for Loop
It is a pre-test loop
x
initialization
°condition
body of loop
increment
y
FALSE
TRUE
Repetition Control Structure (cont…)
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 27
Example:
Start
Stop
ReadLength,Width
PrintArea,
Perimeter
Calculate AreaArea=Length * Width
Calculate PerimeterPerimeter=
2 * (Width+Length)
Input:Length <- 5Width <- 3
Process:Area = 5 * 3 = 15
Process:Perimeter = 2* (5+3) = 16
Output
Area: 15
Perimeter: 16
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 28
Start
Stop
Read Num
Print"Category A"
Yes
Num>0? No
Print"Category B"
Example: What is the output of the following flowchart when the input Num= 10
Num = 1010 > 0 ? => YES
Input:Num <- 10
Enter a Number >> 10
Output:“Category A”
Category A
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 29
Start
Stop
Read Num
Print"Category A"
Yes
Num>0? No
Print"Category B"
Example: What is the output of the following flowchart when the input is Num= 0
Num = 00 > 0 ? => NO
Output:“Category B”
Input:Num <- 0
Enter a Number >> 0
Category B
Output:“Category A”
Category A
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 30
Start
Stop
PrintResult
Result=Result + Count
Count=Count - 1
Initialize
Result=0Count=Num
Count>0?
Read Num
No
Print Count
Yes
Example: What is the output of the following flowchart when the input is Num= 4
Input:Num <- 4
Enter a Number => 4
Variables (in memory):
Num [ ]Result [ ]Count [ ]
Variables (in memory):
Num [ 4 ]Result [ ]Count [ ]
Count = 44 > 0 ? => YES
Variables (in memory):
Num [ 4 ]Result [ 0 ]Count [ 4 ]
Count: 4
Variables (in memory):
Num [ 4 ]Result [ 4 ] 0 + 4Count [ 3 ] 4 - 1
Count: 3
Count = 33 > 0 ? => YES
Variables (in memory):
Num [ 4 ]Result [ 7 ] 4 + 3Count [ 2 ] 3 - 1
Count: 2
Count = 22 > 0 ? => YES
Variables (in memory):
Num [ 4 ]Result [ 9 ] 7 + 2Count [ 1 ] 2 - 1
Count: 1
Count = 11 > 0 ? => YES
Variables (in memory):
Num [ 4 ]Result [ 10] 9 + 1Count [ 0 ] 1 - 1
Count: 0
Count = 00 > 0 ? => NO
Result: 10
Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 |Last Updated: July 2005 Slide 31
Page 1
AVRG (average, 10, 5, N)
Start
Stop
ReadN
Printaverage
Page 2
AVRG ( result,n1, n2,n3)
Return
sum = n1+ n2+n3
result = sum/3
Example: What is the output of the following flowchart when the input is N = 6
average
10
5
N=6
Sum = 10 + 5 + 6
average = 21/3
Output:Average: 7