Algorithms and Flowcharts
Algorithms and Flowcharts
FLOWCHARTS
Summer Assignment
AP Computer Science
Wakefield High School
Assignment Summary
1.
2.
http://courses.cs.vt.edu/csonline/Algorithms/Lessons/i
ndex.html
VA Tech developed the above interactive module
lesson. You need to read thoroughly and play the
games. It gives you a better understanding of how
sorting algorithms are represented.
Implementation phase
implement
language
Detailed Algorithm
Step 1:
Input M1,M2,M3,M4
Step 2:
GRADE (M1+M2+M3+M4)/4
Step 3:
if (GRADE < 50) then
Print FAIL
else
Print PASS
endif
The Flowchart
The Flowchart
A Flowchart
shows
logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
Flowchart Symbols
Basic
Example
START
Input
Q1,Q2,Q3,Q4
GRADE(Q1+Q2+Q3+Q4)/4
IS
GRADE<5
0
PRINT
PASS
PRINT
FAIL
STOP
Example 2
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by
multiplying LFT with 30
Print length in cm (LCM)
Example 2
Flowchart
Algorithm
Step 1: Input Lft
Step 2: Lcm Lft x 30
Step 3: Print Lcm
START
Input
Lft
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
Input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
Print A
Example 3
Algorithm
Step 1: Input W,L
Step 2: A L x W
Step 3: Print A
START
Input
W, L
ALxW
Print
A
STOP
Example 4
Example 4
Pseudocode:
Input the coefficients (a, b, c) of the
quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Example 4
START
Algorithm:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Input a, b, c
d sqrt ( b b 4 a c )
x1 (b + d) / (2 x a)
x2 (b d) / (2 x a)
Print x1, x2
Input
a, b, c
d sqrt(b x b 4 x a x c)
x1 (b + d) / (2 x a)
X2 (b d) / (2 x a)
Print
x1 ,x2
STOP
DECISION STRUCTURES
DECISION STRUCTURES
Print
A
is
A>B
Print
B
IFTHENELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
IFTHENELSE STRUCTURE
The algorithm for the flowchart is as
follows:
If A>B then
Y
N
is
print A
A>B
else
Print
Print
print B
A
B
endif
Relational Operators
Relational Operators
Operator
Description
>
Greater than
<
Less than
==
Equal to
!=
Not equal to
Example 5
Write an algorithm that reads two values, determines the
largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1:
Input VALUE1, VALUE2
Step 2:
if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3:
Print The largest value is, MAX
Example 5
START
Input
VALUE1,VALUE2
is
VALUE1>VALUE2
MAX VALUE1
MAX VALUE2
Print
The largest value is,
MAX
STOP
NESTED IFS
Example 6
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
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
Example 6
Example 7
a)
b)
Example 7
Bonus Schedule
OVERTIME (2/3)*ABSENT Bonus Paid
>40 hours
>30 but 40 hours
>20 but 30 hours
>10 but 20 hours
10 hours
$50
$40
$30
$20
$10
2.
3.
You want badly to take a friend out for a movie and dinner. And
guess what? Your friend gladly accepted but you are broke. Now
you have 36 hours (3 days) to get $100 and avoid embarrassment.
Your sources are a last minute neighbors mow yard, parents,
relatives, or else.
4.