Lect02 2 Introtoalgo PDF
Lect02 2 Introtoalgo PDF
1
Topics
2
The Problem-solving Process
"Doctor, my head hurts"
Implementation New=NewWire(length,amp);
Replace(old,new);
check(TV);
Program /* etc. etc. */
Compilation
01001110101100101010101010010
10101010100110010101010101001
011010011101010101010010010111
Executable 010011110101010111110101010001
(solution) 10100001101...
5
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution) 6
Algorithm – Working Definition
• A sequence of instructions describing how
to do a task
7
Algorithm -- Examples
• A cooking recipe
• The rules of how to play a game
• Projector instructions
• Description of a martial arts technique
• Directions for driving from A to B
• A car repair manual
8
Algorithm – Examples (cont)
• Recipe for Almond and honey slice
• Rose milk shake
9
Almond and Honey Slice Preheat oven for 200° C
Line a 30 cm 20 cm baking
tray with baking foil paper,
1/2 quantity Shortcrust
and then with pastry
Pastry
Bake blind for 20 minutes, then
185 g unsalted butter
remove foil paper
100 g sugar
Turn oven up to 220° C.
5 tablespoons honey
Bring remaining ingredients to a
50 ml cream boil, stirring.
50 ml brandy or spirit Spread evenly over pastry.
300 g flaked almonds Bake until topping is bubbling,
about 15 minutes.
From: Stephanie Alexander, The Cool before cutting into fingers
Cook’s Companion, Viking/Penguin, or squares.
Ringwood, Victoria, 1996, p. 349.
10
Rose milk shake Pore milk to mixer jar
C Program 13
Components of an Algorithm
• Variables and values
• Instructions
– Sequences
– Procedures
– Selections
– Repetitions
Also required: Documentation
14
Values
• Represent quantities, amounts or
measurements
• May be numerical or alphabetical
(or other things)
• Often have a unit related to their purpose
• Example:
– Recipe ingredients
15
Rose milk shake
Pore milk to mixer jar
200 ml rose syrup
Add rose syrup to jar
1 ltr milk
100 g sugar Add sugar
150 g Ice cubes Switch on the mixer switch and
mix them for 30 seconds
Add ice cubes to the jar and
again switch the mixer on for
30 seconds
Pore the mix in glass and serve
16
Rose milk shake
Pore milk to mixer jar
200 ml rose syrup
Add rose syrup to jar
1 ltr milk
100 g sugar Add sugar
150 g Ice cubes Switch on the mixer switch and
mix them for 30 seconds
Add ice cubes to the jar and
again switch the mixer on for
30 seconds
Pore the mix in glass and serve
17
Variables
• Are containers for values – places to store
values
• Example:
Variable Values
19
Components of an Algorithm
Values and Variables
• Instruction (a.k.a. primitive)
– Sequence (of instructions)
– Procedure (involving instructions)
– Selection (between instructions)
– Repetition (of instructions)
• Documentation (beside instructions)
20