01 On Programming
01 On Programming
Suppose f is monotonically
increasing on [a, b] and:
f(a) ≤ 0, f(b) ≥ 0
To determine the x-intersect with
precision ε > 0:
An x-intersect of function f is
1. as long as b – a > ε, do 2. – 4.
an x such that f(x) = 0.
2. calculate m = (a + b)/2
3. if f(m) ≤ 0, set a to m, or
4. if f(m) ≥ 0, set b to m
The result is (a, b) such that
f(a) ≤ 0, f(b) ≥ 0, b – a ≤ ε
¡ Declarative descriptions state the properties of the result.
They describe what the result is. They are short, but cannot
be followed.
§ In the case of x-intersect, there could be several possible
results or their could be none
No, for some f the “precise algorithm” will not stop: if a, b are
rational and the x-intersect is irrational, it be never be reached.
An algorithm specifies a sequence of instructions to be
executed by a machine that, when provided with input, will
eventually stop and produce some output.
Assume that positive integers x and y are given. Trace for input x = 28 and y = 42:
1. Let u be x and v be y.
Instruction u v
2. As long as u is not equal to v, do one of
the following: 1. 42
28
2.1. If u > v, then subtract v from u.
2.2. If v > u, then subtract u from v. 2.2. 28 14
Finally, u is equal to v and their value is the
greatest common divisor of x and y. 2.1. 14 14
A S T End
(optional):
Sequence: Repetition:
– if B then S while B do S
B –
S + B
+
S then T
S
T S
The basic instructions of flowcharts are those of underlying
machine has for changing the state. If the machine is a
computer, the basic instruction is the assignment to a variable
x := e x becomes e
¡ This avoids
spaghetti-like
flowcharts!
https://en.wikipedia.org/wiki/Flowchart
Patient
http://www.smartdraw.com/flowchart/flowchart-software.htm
leaves
https://www.gliffy.com/examples/flow-charts/
The median of x, y, z, is the “middle” number. The median of 3, 7, 2
is 3. The median of 3, 7, 3 is also 3. Assign the median of x, y, z to m!
+ –
x<y
+ – + –
z<x x<z
+ – + –
m := x z<y m := x z<y
m := z m := y m := y m := z
• Annotations express
what holds at particular
points
+ –
x<y
x<y • They are declarative
z<x + – x<y descriptions
z<x
x<y x≤z
+ – • They help understanding
m := x z<y x<y
algorithms and arguing
y≤z
their correctness
m := z m := y
z<m x<m
m<y m≤z • It is good practice to
explicitly write the main
annotations
i := i + 1
¡ How many comparisons
with ti will be made in i, min, max := 1, t0, t0
the best and worst case? –
¡ n – 1 comparisons i<n
if t is strictly decreasing +
¡ 2 x (n – 1) comparisons if + –
ti < min
t is increasing
–
¡ How many assignments min := ti ti > max
to min and max will be +
made in the best and max := ti
worst case!
¡ 1 to min, 1 to max if all ti
are the same i := i + 1
¡ 1 + n in total if t strictly
increasing
1. At check-in counter: • Reformulate as an algorithm!
§ Present id
§ Put luggage on scale. If too • Verbs indicate instructions.
heavy pay fee or remove
items and put luggage back
on scale • Conditions suggest selection
§ Obtain boarding pass
and repetition
2. At security check:
§ Place necklace, belt, etc., in • Although this is presented in
tray “structured English”, look for
§ Pass through scanner synonyms and implicit
§ If scanner beeps check for information.
further accessories and try
again
3. At the gate:
§ Present boarding pass
Go to
1. At check-in counter: • Reformulate as an algorithm!
§ Present id
§ Put luggage on scale. If too • Verbs indicate instructions.
heavy pay fee or remove
items and put luggage back
on scale • Conditions suggest selection
and repetition
Go to§ Obtain boarding pass
2. At security check:
§ Place necklace, belt, etc., in • Although this is presented in
tray “structured English”, look for
§ Pass through scanner synonyms and implicit
§ If scanner beeps check for information.
further accessories and try
again
Go to
3. At the gate:
§ Present boarding pass
Go to check-in counter
Present passport
function gcd(x, y: integer): integer; int gcd(int x, int y) { def gcd(x, y):
var u, v: integer; int u, v; u, v = x, y
begin u := x; v := y; u = x; v = y; while u != v:
while u <> v do while (u != v) if u > v: u = u – v
if u > v then u := u – v if (u > v) u = u – v; else: v = v – u
else v := v – u; else v = v – u; return u
gcd := u return u;
end }
u, v := x, y
u≠v –
+
+ u>v –
u := u - v v := v - u
¡ Machine instructions are very primitive:
§ Move data between memory and processor registers
§ Perform an arithmetic operation on registers
§ Compare registers
§ Jump to another memory location
u := u – v R1 := u move u to register 1
R2 := v move v to register 2
R1 := R1 – R2 subtract register 2 from 1
u := R1 move register 1 to u
¡ Notation:
§ <> or != for ≠
§ begin … end or {…} or indentation for bracketing
¡ Safety:
§ type checking (Java: static, Python: dynamic, C: little)
¡ Support for large programs:
§ classes (Java, Python: yes, C: no)
§ exceptions (Java, Python: yes, C: no)
¡ Instructions and standard libraries:
§ data types like lists, sets, dictionaries (Python: yes)
§ interaction, graphics (mixed)
¡ Application:
§ dedicated (JavaScript, Flash: web pages)
§ general-purpose (Java, Python, Pascal, C: yes)
¡ The Python notation is compact and intuitive,
making it a good beginner’s language