Ics-Slide5-Flowchart and Pseudocode
Ics-Slide5-Flowchart and Pseudocode
3
Using Pseudocode Statements
and Flowchart Symbols
4
Writing Pseudocode
5
Example
• Example : Write a pseudocode to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
Pseudocode
Pseudocode:
start
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
stop
Flowchart
Using Flowchart Symbols
• Input symbol
o Indicates input operation
o Parallelogram
9
Using Flowchart Symbols
• Processing symbol
o Processing statements such as
arithmetic
o Rectangle
10
Using Flowchart Symbols
• Output symbol
o Represents output statements
o Parallelogram
11
Drawing Flowcharts (continued)
• Flowlines
– Arrows that connect steps
• Terminal symbols
– Start/stop symbols
– Shaped like a racetrack
12
Flowchart Symbols and Pseudocode Statements
Step 1 : Start
Step 2 : Input first number A
Step 3 : Input second number B
Step 4 : Add the two numbers and
store it in total
Step 5 : Print Total
Step 6 : Stop
12-Feb-15 16
Problem
Write a pseudocode and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Solution
• . Start
Pseudocode
Output A
End
Problem
• Write a pseudocode and flowchart to find the
area of a square.
Flowchart: Calculating area of a square
Start Pseudocode
Step 1 : Start
Input x Step 2 : Read value for x (side)
Step 3 : [Compute] Area = x * x
Step 4 : Output Area
Step 5 : Stop
Area = x*x
Output Area
End
20
Problem
Write a flowchart and pseudocode to find the
average of three numbers.
Flowchart: Find the average of three numbers
Flowchart Pseudocode
12-Feb-15 22
Understanding the Three Basic Structures
• Structure
– Basic unit of programming logic
– Sequence
• Perform actions in order
• No branching or skipping any task
– Selection (decision)
• Ask a question, take one of two actions
• Dual-alternative or single-alternative ifs
– Loop
• Repeat actions based on answer to a question
23
Understanding the Three Basic Structures
Sequence structure
24
Understanding the Three Basic Structures
(continued)
Selection structure
25
Understanding the Three Basic Structures
(continued)
• Dual-alternative if
– Contains two alternatives
– If-then-else structure
if someCondition is true then
do oneProcess
else
do theOtherProcess
26
Understanding the Three Basic Structures
(continued)
• Single-alternative if
if student answers the bonus questions correctly
then add extra 5 marks
27
Understanding the Three Basic Structures
(continued)
28
Flow chart to find the larger of two numbers.
Pseudocode
Step1: Start
Step 2: Enter two numbers A and B
Step 3: Check if A is greater than B if yes go to
Step 4 else go to Step 5
Step 4: Print A is greater than B
Step 5: Check if B is greater than A if yes go to
Step 6 else go to Step 7
Step 6: Print B is greater than A
Step 7: Print A is equal to B
Step 8: Stop
29
Problem
• Draw a flowchart to find out if a number is
even or odd.
Solution
yes no
Problem
Draw a flow chart to find out if a number is
divisible by 5 or not?
.
Start
Input x
no x MOD 5 == yes
0?
End
Start
Decision Problem# 3
Input
x,y,z
no yes
Is y > z Is x > y Is x > z
yes no no yes
Output z
Output y Output x
End
Repetition in programming
• Sometimes we have to repeat some tasks in a
program
• Example: adding the same number multiple
times
• This is called a repetition
Loop Structure
• Loop structure
– Repeats a set of actions based on the answer to a
question
• Loop body
– Also called repetition or iteration
– Question is asked first in the most common form
of loop
– while … for
Loop Structure
Example
sum = 0
for i = 1 to 100
sum = sum + I
What are we trying to do?
Example
• while testCondition continues
to be true
do someProcess
i sum
- 0
1 1
2 3
3 6
4 6
Example
Example
Find the output for n=4
Practice problem