Title: DCP2073 Asas Pengaturcaraan C Lecture 2: Introduction to Algorithms
1DCP2073Asas Pengaturcaraan CLecture
2Introduction to Algorithms
2Topics
- A little bit of History
- The idea of a universal machine
- Systematic problem solving
- Algorithms
3A Universal Machine
- Programmability
- Binary Code (everything stored as 0 or 1)
- Program and Data are represented in the same
form - Electronic Components
4How do we solve problems?
- We "just do"
- Guesswork-and-luck
- Trial-and-error
- Experience (possibly someone else's)
- "Scientifically"
5The Problem-solving Process
"Doctor, my head hurts"
Patient has elevated pressure in anterior
parietal lobe
1. Sterilize cranial saw 2. Anaesthetize
patient 3. Remove top of skull 4. Get the big
spoon... 5. etc., etc.
sterilize(saw,alcohol) raise_hammer() lower
hammer(fast) start(saw) / etc. etc. /
01001110101100101010101010010101010101001100101010
10101001011010011101010101010010010111010011110101
010111110101010001101
6The Problem-solving Process
"Doctor, my head hurts"
Patient has elevated pressure in anterior
parietal lobe.
Analysis
Problem specification
1. Sterilize cranial saw 2. Anaesthetize
patient 3. Remove top of skull 4. Get the big
spoon... 5. etc., etc.
Design
Algorithm
Implementation
sterilize(saw,alcohol) raise_hammer() lower
hammer(fast) start(saw) / etc. etc. /
Program
Compilation
01001110101100101010101010010101010101001100101010
10101001011010011101010101010010010111010011110101
01011111010101000110100001101...
Executable (solution)
7The Problem-solving Process
Analysis
Problem specification
Design
Algorithm
Implementation
Program
Compilation
Executable (solution)
8Algorithm
- A sequence of instructions specifying the steps
required to accomplish some task - Named after Muhammad ibn Musa al-Khwarizmi
- of Khowarezm (now Khiva in Uzbekistan)
- Circa 780-850 C.E. (Common Era)
9Algorithm History
- Muhammad ibn Musa Al-Khwarizmi
- http//www-groups.dcs.st-andrews.ac.uk/history/Ma
thematicians/Al-Khwarizmi.html - Book on arithmetic
- Hindu numeration, decimal numbers, use of zero,
method for finding square root - Latin translation (c.1120 CE) Algoritmi de
numero Indorum - Book on algebra
- Hisab al-jabr wal-muqabala
10Algorithm Working Definition
- A sequence of instructions describing how to do a
task
As opposed to actually executing the
instructions
11Algorithm -- Examples
- A cooking recipe
- Assembly instructions for a model
- The rules of how to play a game
- VCR instructions
- Description of a martial arts technique
- Directions for driving from A to B
- A knitting pattern
- A car repair manual
12Algorithm Examples (cont)
- Recipe for Almond and honey slice
- Recipe for Arroz con pollo
13Almond and Honey Slice
- Preheat oven for 200 C
- Line a 30 cm ? 20 cm baking tray with baking
paper, and then with pastry - Bake blind for 20 minutes, then remove weights
and foil - Turn oven up to 220 C.
- Bring remaining ingredients to a boil, stirring.
- Spread evenly over pastry.
- Bake until topping is bubbling and has
caramelised evenly, about 15 minutes. - Cool before cutting into fingers or squares.
- 1/2 quantity Shortcrust Pastry
- 185 g unsalted butter
- 100 g castor sugar
- 5 tablespoons honey
- 50 ml cream
- 50 ml brandy or any other liqueur or spirit
- 300 g flaked almonds
From Stephanie Alexander, The Cooks Companion,
Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
14Almond and Honey Slice
- Preheat oven for 200 C
- Line a 30 cm ? 20 cm baking tray with baking
paper, and then with pastry - Bake blind for 20 minutes, then remove weights
and foil - Turn oven up to 220 C.
- Bring remaining ingredients to a boil, stirring.
- Spread evenly over pastry.
- Bake until topping is bubbling and has
caramelised evenly, about 15 minutes. - Cool before cutting into fingers or squares.
- 1/2 quantity Shortcrust Pastry
- 185 g unsalted butter
- 100 g castor sugar
- 5 tablespoons honey
- 50 ml cream
- 50 ml brandy or any other liqueur or spirit
- 300 g flaked almonds
Instructions are given in the order in which they
are performed (executed)
From Stephanie Alexander, The Cooks Companion,
Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
15Correct Algorithm?
- Cut chicken into pieces and brown the pieces on
all sides in a casserole dish in hot olive oil. - Remove the chicken and to the juices in the
casserole add garlic, onions and green peppers,
and sauté until onion is golden. - Add bay leaf, whole tomatoes, and chicken broth.
- When the broth boils add salt, saffron and rice.
- Arrange chicken on rice, cover casserole and bake
in a moderate oven (350F) for 20 minutes or
until the rice is tender. - Add beans and artichokes during last 10 minutes
of cooking.
From Arroz Con Pollo in The Margaret Fulton
Cookbook, Hamlyn, Sydney, 1968.
16Correct Algorithm?
- Cut chicken into pieces and brown the pieces on
all sides in a casserole dish in hot olive oil. - Remove the chicken and to the juices in the
casserole add garlic, onions and green peppers,
and sauté until onion is golden. - Add bay leaf, whole tomatoes, and chicken broth.
- When the broth boils add salt, saffron and rice.
- Arrange chicken on rice, cover casserole and bake
in a moderate oven (350F) for 20 minutes or
until the rice is tender. - Add beans and artichokes during last 10 minutes
of cooking.
From Arroz Con Pollo in The Margaret Fulton
Cookbook, Hamlyn, Sydney, 1968.
17Correct Algorithm?
- Cut chicken into pieces and brown the pieces on
all sides in a casserole dish in hot olive oil. - Remove the chicken and to the juices in the
casserole add garlic, onions and green peppers,
and sauté until onion is golden. - Add bay leaf, whole tomatoes, and chicken broth.
- When the broth boils add salt, saffron and rice.
- Arrange chicken on rice, cover casserole and bake
in a moderate oven (350F) for 10 minutes. - Add beans and artichokes.
- Cover, and bake for another 10 minutes or until
rice is tender.
18From Algorithms to Programs
19Components of an Algorithm
- Variables and values
- Instructions
- Sequences
- Procedures
- Selections
- Repetitions
- Also required Documentation
20Values
- Represent quantities, amounts or measurements
- May be numerical or alphabetical (or other
things) - Often have a unit related to their purpose
- Example
- Recipe ingredients
21Almond and Honey Slice
- Preheat oven for 200 C
- Line a 30 cm ? 20 cm baking tray with baking
paper, and then with pastry - Bake blind for 20 minutes, then remove weights
and foil - Turn oven up to 220 C.
- Bring remaining ingredients to a boil, stirring.
- Spread evenly over pastry.
- Bake until topping is bubbling and has
caramelised evenly, about 15 minutes. - Cool before cutting into fingers or squares.
- 1/2 quantity Shortcrust Pastry
- 185 g unsalted butter
- 100 g castor sugar
- 5 tablespoons honey
- 50 ml cream
- 50 ml brandy or any other liqueur or spirit
- 300 g flaked almonds
From Stephanie Alexander, The Cooks Companion,
Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
22Almond and Honey Slice
- Preheat oven for 200 C
- Line a 30 cm ? 20 cm baking tray with baking
paper, and then with pastry - Bake blind for 20 minutes, then remove weights
and foil - Turn oven up to 220 C.
- Bring remaining ingredients to a boil, stirring.
- Spread evenly over pastry.
- Bake until topping is bubbling and has
caramelised evenly, about 15 minutes. - Cool before cutting into fingers or squares.
- 1/2 quantity Shotcrust Pastry
- 185 g unsalted butter
- 100 g castor sugar
- 5 tablespoons honey
- 50 ml cream
- 50 ml brandy or any other liqueur or spirit
- 300 g flaked almonds
From Stephanie Alexander, The Cooks Companion,
Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
23Variables
- Are containers for values places to store
values - Example
24Restrictions on Variables
- Variables may be restricted to contain a specific
type of value
25Components 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)
26Summary
- Problem Solving Process
- Algorithms
- Components of Algorithms
- Values and Variables
- Instructions
- to be continued...
Reading
- Deitel Deitel, C How to program
- Chapter 3, Sections 3.1 to 3.3