2/11/2014
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into
ALGORITHMS AND two phases:
Problem solving phase
FLOWCHARTS produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
Implementation phase
Implement the program in some programming
language
2/11/2014 EEEQ 118 1 2/11/2014 EEEQ 118 2
Steps in Problem Solving Pseudocode & Algorithm
First produce a general algorithm (one can use Example 1: Write an algorithm to
pseudocode)
determine a student’s final grade and
Pseudocode is an artificial and informal language
that helps programmers develop algorithms. indicate whether it is passing or failing.
Pseudocode is very similar to everyday English. The final grade is calculated as the
Refine the algorithm successively to get step by average of four marks.
step detailed algorithm that is very close to a
computer language.
2/11/2014 EEEQ 118 3 2/11/2014 EEEQ 118 4
Pseudocode & Algorithm Pseudocode & Algorithm
Pseudocode: Detailed Algorithm
Input a set of 4 marks Step 1: Input M1,M2,M3,M4
Calculate their average by summing and dividing Step 2: GRADE (M1+M2+M3+M4)/4
by 4 Step 3: if (GRADE < 50) then
if average is below 50 Print “FAIL”
Print “FAIL” else
else Print “PASS”
Print “PASS” endif
2/11/2014 EEEQ 118 5 2/11/2014 EEEQ 118 6
1
2/11/2014
The Flowchart The Flowchart
(Dictionary) A schematic representation of a sequence of A Flowchart
operations, as in a manufacturing process or computer
program. shows logic of an algorithm
(Technical) A graphical representation of the sequence emphasizes individual steps and their
of operations in an information system or program.
interconnections
Information system flowcharts show how data flows from
source documents through the computer to final e.g. control flow from one action to the next
distribution to users.
Program flowcharts show the sequence of instructions in
a single program or subroutine. Different symbols are
used to draw each type of flowchart.
2/11/2014 EEEQ 118 7 2/11/2014 EEEQ 118 8
Flowchart Symbols Example
Basic START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
2/11/2014 EEEQ 118 9 2/11/2014 EEEQ 118 10
Example 2 Example 2
Flowchart
Write an algorithm and draw a flowchart to Algorithm START
convert the length in feet to centimeter. Step 1: Input Lft
Pseudocode: Input
Lft
Step 2: Lcm Lft x 30
Input the length in feet (Lft)
Step 3: Print Lcm Lcm Lft x 30
Calculate the length in cm (Lcm) by
multiplying LFT with 30 Print
Lcm
Print length in cm (LCM)
STOP
2/11/2014 EEEQ 118 11 2/11/2014 EEEQ 118 12
2
2/11/2014
Example 3 Example 3
Write an algorithm and draw a flowchart that Algorithm START
will read the two sides of a rectangle and Step 1: Input W,L Input
calculate its area. W, L
Step 2: A L x W
Pseudocode
Input the width (W) and Length (L) of a rectangle
Step 3: Print A A L x W
Calculate the area (A) by multiplying L with W
Print
A
Print A
STOP
2/11/2014 EEEQ 118 13 2/11/2014 EEEQ 118 14
Example 4 Example 4
Write an algorithm and draw a flowchart that Pseudocode:
will calculate the roots of a quadratic equation Input the coefficients (a, b, c) of the
ax 2 bx c 0 quadratic equation
Hint: d = sqrt ( b 2 4ac ), and the roots are: Calculate d
x1 = (–b + d)/2a and x2 = (–b – d)/2a Calculate x1
Calculate x2
Print x1 and x2
2/11/2014 EEEQ 118 15 2/11/2014 EEEQ 118 16
Example 4 DECISION STRUCTURES
START
Algorithm: The expression A>B is a logical expression
Input
Step 1: Input a, b, c a, b, c it describes a condition we want to test
Step 2: d sqrt ( b b 4 a c )
if A>B is true (if A is greater than B) we take
Step 3: x1 (–b + d) / (2 x a) d sqrt(b x b – 4 x a x c)
the action on left
Step 4: x2 (–b – d) / (2 x a)
Step 5: Print x1, x2
x1 (–b + d) / (2 x a) print the value of A
X2 (–b – d) / (2 x a) if A>B is false (if A is not greater than B) we
Print
take the action on right
x1 ,x2
print the value of B
STOP
2/11/2014 EEEQ 118 17 2/11/2014 EEEQ 118 18
3
2/11/2014
DECISION STRUCTURES IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
Y N
is
A>B
true alternative
else
Print Print
A B false alternative
endif
2/11/2014 EEEQ 118 19 2/11/2014 EEEQ 118 20
IF–THEN–ELSE STRUCTURE Relational Operators
The algorithm for the flowchart is as Relational Operators
follows: Operator Description
If A>B then > Greater than
Y N
print A is
A>B
< Less than
else = Equal to
Greater than or equal to
print B Print
A
Print
B Less than or equal to
endif Not equal to
2/11/2014 EEEQ 118 21 2/11/2014 EEEQ 118 22
Example 5 Example 5
START
Write an algorithm that reads two values, determines the
Input
largest value and prints the largest value with an VALUE1,VALUE2
identifying message.
ALGORITHM
Y N
Step 1: Input VALUE1, VALUE2 is
VALUE1>VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
MAX VALUE1 MAX VALUE2
else
MAX VALUE2
endif Print
Step 3: Print “The largest value is”, MAX “The largest value is”,
MAX
STOP
2/11/2014 EEEQ 118 23 2/11/2014 EEEQ 118 24
4
2/11/2014
NESTED IFS Example 6
One of the alternatives within an IF– Write an algorithm that reads three
THEN–ELSE statement numbers and prints the value of the largest
may involve further IF–THEN–ELSE number.
statement
2/11/2014 EEEQ 118 25 2/11/2014 EEEQ 118 26
Example 6 Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then Flowchart: Draw the flowchart of the
MAX N1 [N1>N2, N1>N3]
else
above Algorithm.
MAX N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
2/11/2014 EEEQ 118 27 2/11/2014 EEEQ 118 28
Example 7 Example 7
Write and algorithm and draw a flowchart Bonus Schedule
to
OVERTIME – (2/3)*ABSENT Bonus Paid
a) read an employee name (NAME),
overtime hours worked (OVERTIME),
>40 hours $50
hours absent (ABSENT) and >30 but 40 hours $40
b) determine the bonus payment >20 but 30 hours $30
(PAYMENT). >10 but 20 hours $20
10 hours $10
2/11/2014 EEEQ 118 29 2/11/2014 EEEQ 118 30
5
2/11/2014
Step 1: Input NAME,OVERTIME,ABSENT Example 7
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
else if (OVERTIME–(2/3)*ABSENT > 30) then Flowchart: Draw the flowchart of the
PAYMENT 40 above algorithm?
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT 10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
2/11/2014 EEEQ 118 31 2/11/2014 EEEQ 118 32