Algorithms Presentation
Algorithms Presentation
What is an Algorithm?
Algorithm characteristics
Elements of an algorithm
Expressing algorithms
Pseudocode
Flowcharting
Examples
What is an Algorithm?
3
Solving a maze
Input
Output
Precision
Finiteness
Uniqueness
Generality
Algorithm Elements (1/4)
8
Selection
Iteration
Algorithm Elements (2/4)
9
Sequence
Selection
Iteration
Output
Expressing Algorithms
12
languages
Two tools commonly used
Pseudocode
Flowchart
Pseudocode
13
Combination of graphical
input/output
symbols that represent
logical flow of data thro’
solution sequence
decision
terminal
flowline
Pseudocode: Input
15
1: read x input x
1: read a, b, c input a, b, c
Pseudocode: Output
16
1: print x output x
output
1: print “your speed is: “, x
“your speed is: “, x
Sequence (1/2)
17
x = 10
1: x = 10
2: y = ax2+bx+c y=ax2+bx+c
3: z = y
z=y
Statements, Expressions, … (1/4)
19
assignment operator
variable x = 10 constant
operand
Statements, Expressions, … (2/4)
20
3 1: x = 10
5 2: y = x*x
7 3: z = x + y * y
Statements, Expressions, … (3/4)
21
1: z is integer assignment
2: z = 10 = operator
type is
z 10 integer
Statements, Expressions, … (4/4)
22
variable = expression
variable expression
Arithmetic Operators
24
+, -, *, /, %
=
op
variable
operand1 operand2
Relational Operators
25
condition condition
true true false
false
action action 1 action 2
𝒂% Letter Grade
𝑎 ≥ 93 A
𝑎 ≥ 93 90 ≤ 𝑎 < 93 A−
true false 87 ≤ 𝑎 < 90 B+
83 ≤ 𝑎 < 87 B
print ‘A’ print not ‘A’ 80 ≤ 𝑎 < 83 B−
77 ≤ 𝑎 < 80 C+
73 ≤ 𝑎 < 77 C
70 ≤ 𝑎 < 73 C−
60 ≤ 𝑎 < 70 D
𝑎 < 60 F
Selection Structure (4/4)
30
𝑎 ≥ 93
true false
print ‘A’ 𝑎 ≥ 90
true false
print ‘A-’ 𝑎 ≥ 87
true false
print ‘B+’ 𝑎 ≥ 83
true false
print ‘B’ ⋮
true false
Iteration Element
31
Compute 𝑥 2 ∀ 𝑥 ∈ 1, 𝑁 𝑠𝑢𝑚 = 0
𝑥=1
1: read 𝑁
2: 𝑠𝑢𝑚 = 0
𝑓𝑎𝑙𝑠𝑒
𝑥≤𝑁
3: 𝑥 = 1 𝑡𝑟𝑢𝑒
4: while (𝑥 <= 𝑁)
𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑥 2
5: 𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑥 ∗ 𝑥 𝑥 =𝑥+1
6: 𝑥 = 𝑥 + 1
7: endwhile
output 𝑠𝑢𝑚
What is an Algorithm?
Algorithm characteristics: inputs, outputs, precision,
finiteness, uniqueness, generality
Elements of an algorithm: sequence, selection, iteration
Expressing algorithms: Pseudocode, flowcharting
Sequence: operator, operand, expression, variable,
constant, type, integer and floating-point values,
assignment operator, arithmetic operator, relational
operators
Examples
Things to do: Read handout, complete assignment