# Intro to Computer Programming Algorithms Lecture # 01 01/21/05

## Presentation on theme: "Intro to Computer Programming Algorithms Lecture # 01 01/21/05"— Presentation transcript:

Intro to Computer Programming Algorithms Lecture # 01 01/21/05
B Smith: Required 45 to 50 minutes. Stirred up few questions. Needs to be more interactive and less general/abastract. Score 3 with improv, 2.5 if used as is. B Smith: Not used Fall05. Not needed; not helpful Math 130 Intro to Computer Programming Algorithms Lecture # 01 01/21/05 B Smith: Great for independent study; not so great otherwise. Algorithm 1: The Morning Routine Get out of bed Shower Bathe kids Prepare cereal .

Overview Solving problems Algorithms Values and variables
mysterious or methodical luck or by design Algorithms recipes Values and variables quantity and place holders

How do we solve problems?
Math 130 Intro to Computer Programming 4/12/2017 1:07 PM How do we solve problems? Largely empirical “Doing heart surgery in this manner works” History “This is how Alice did it…” Inspiration “I don’t know how, but I solved it!” Brute force Trial-and-error The scientific method Largely empirical: Partly inspiration Empirical: build on what’s been learned History: Simply copy someone else’s method Inspiration: Luck! Brute force: Scientific Method: empirical and theoretical. Combines all the above. B. Smith

The Problem-solving Process
Math 130 Intro to Computer Programming B Smith: Use math as a useful application. How about finding the roots of 2x^2+3x-4 = 0? What would be the algorithm. 4/12/2017 1:07 PM The 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. */ Pbm - We initially start with a fuzzy idea of the problem and associated issues Spec - Putting it down on paper or communicating the problem to someone else coerces more clarity (hopefully) Problem solving is often not linear and sequential; once we get to the end, we may decide that we need to review steps. Or even during the process of problem-solving, we may need to keep looking back at our footsteps. Program Compilation Executable (solution) B. Smith

The Problem-solving Process
Math 130 Intro to Computer Programming 4/12/2017 1:07 PM The Problem-solving Process Analysis Problem specification Design Algorithm Implementation At the center of all this is the “algorithm.” Program Compilation Executable (solution) B. Smith

Math 130 Intro to Computer Programming
4/12/2017 1:07 PM Algorithm A sequence of instructions specifying the steps required to accomplish some task Named after: Muhammad ibn Mūsā al-Khwārizmi Circa C.E. (Common Era) From D Knuth, TAOCP. Persian textbook author, c. 825 B. Smith

Math 130 Intro to Computer Programming
4/12/2017 1:07 PM Algorithm –History Muhammad ibn Musa al-Khwarizmi 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 “Kitāb al-jabr wa’l-muqābala” (“Rules of restoring and equating”) B. Smith

Algorithm – Working Definition
A sequence of instructions describing how to do a task As opposed to actually executing the instructions!

The Problem-solving Process
Math 130 Intro to Computer Programming 4/12/2017 1:07 PM The 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. */ Recall the algorithm shown here: This is not a group of actions, but rather a group of instructions to act Program Compilation Executable (solution) B. Smith

Math 130 Intro to Computer Programming
4/12/2017 1:07 PM Algorithm -- Examples A cooking recipe Assembly instructions for a model car, boat, etc. 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 Note emphasis on sequence of instructions. A recipe and the final item are not the same; the recipe is simply the sequence. B. Smith

Algorithm – Examples (cont)
Recipe for Almond and honey slice Recipe for Arroz con pollo

Bake blind for 20 minutes, then remove weights and foil
Almond 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 caramelized 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 Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Math 130 Intro to Computer Programming
4/12/2017 1:07 PM Almond 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 caramelized 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”) To “bake blind” is an English term. See: How to... Bake blind by Terry Farris ‘Baking blind’ means cooking a pastry case without any filling. By baking the pastry before adding the contents, you're guaranteed a crisp, non-soggy pie case How many times has your home-made quiche or tart fallen apart because of a soggy pie case? The simple way to avoid this common problem is blind baking: fully pre-bake the pastry then cook the pie or tart again with the filling in place. … NB: Pie weights are used to give the crust its shape during the “blind baking”. the pie weights could be ceramic, metal, etc. See From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349. B. Smith

Correct Algorithm? Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil. Remove the chicken; 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 (350°F) 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.

Correct 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 (350°F) 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.

Correct 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 (350°F) for 10 minutes. Add beans and artichokes. Cover, and bake for another 10 minutes or until rice is tender.

From Algorithms to Programs
Algorithm: A sequence of instructions describing how to do a task (or process) Problem C Program

Components of an Algorithm
Variables and values Instructions Sequences Procedures Selections Repetitions Documentation

Math 130 Intro to Computer Programming
4/12/2017 1:07 PM Values Represent quantities, amounts, rates, etc. May be numerical, alphabetical or even other items A related unit may or may not be specified Add 3 cups of flour Pull rip-cord Example: Recipe ingredients (how big, how hot, how long) Note distinction b/t values and quantities. Values are dimensions, temperatures, etc. B. Smith

Bake blind for 20 minutes, then remove weights and foil
Almond 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 Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Bake blind for 20 minutes, then remove weights and foil
Almond 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 Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Variables Are containers for values – places to store values Example:
This jar can contain 10 cookies 50 grams of sugar 3 slices of cake etc. Values Variable

Restrictions on Variables
Variables may be restricted to contain a specific type of value

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)

Instructions (Primitives)
Some action that is simple... ...and unambiguous... ...that the system knows about... ...and should be able to actually do

Instructions – Examples
Math 130 Intro to Computer Programming 4/12/2017 1:07 PM Instructions – Examples Directions to perform specific actions on values and variables. Take off your shoes Count to 10 Cut along dotted line Knit 1 Purl 2 Firmly Pull rip-cord Sift 10 grams of sulfur B. Smith

Instructions -- Application
Some instructions can only be applied to specific types of values or variables Examples: Chop

Instructions (Primitives) -- Recommendations
When writing an algorithm, make each instruction simple and unambiguous Example: Cut chicken into pieces. Heat olive oil in a casserole dish. Brown the chicken pieces in the casserole dish. Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Instruction (Primitives)
When writing an algorithm, make the instructions simple and unambiguous. Example: A “sequence” of simple instructions Cut chicken into pieces. Heat olive oil in a casserole dish. Brown the chicken pieces in the casserole dish. Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Math 130 Intro to Computer Programming
Summary 4/12/2017 1:07 PM Problem Solving Process Algorithms Components of Algorithms Values and Variables Instructions to be continued... Reading Bronson, Ch 1 and Ch 2 B. Smith