Title: Al-Khwarizmi 2 (Lecture 3 of CSE1301)
1Al-Khwarizmi 2(Lecture 3 of CSE1301)
- Dr Damian Conway
- Room 132, Building 26
- (Consult hours on the door)
2Algorithms 2(Lecture 3 of CSE1301)
- Dr Damian Conway
- Room 132, Building 26
- (Consult hours on the door)
3Notice
- Monash is helping develop some national tests for
assessing the skills acquired by graduates. - Initial versions of these tests will be trialed
at the end of this month. - Over the next week, many of you will receive a
message asking for volunteers for these trials. - Those messages will be sent to your Monash
student email addresses. - Please check your Monash email regularly over the
next week or so.
4Recall
- What is the problem solving process?
- What is an algorithm?
- What are some examples?
- What are the "variables" in the Australian
Cricket team?
5Topics
- More components of an algorithm
- The software development process
- Top-down design
- Conway's Game of Life
6Components of an algorithm
- What kind of instructions do we give?
- What are the building blocks of those
instructions?
7Components of an algorithm
- Instruction
- Sequence (of instructions)
- Procedure (involving instructions)
- Selection (between instructions)
- Repetition (of instructions)
- Documentation (beside instructions)
8Instruction
- Some action that is simple...
- ...and unambigous...
- ...that the system knows about...
- ...and should be able to actually do.
9Instruction
- Examples
- Take off your shoes.
- Count to 10.
- Cut along dotted line.
- Knit 1.
- Purl 2.
- Pull rip-cord firmly.
- Sift 10 grams of arsenic into the batter.
10Sequence
- A series of instructions
- ...to be carried out one after the other...
- ...without hestitation or question.
- Example
- Cooking a Gourmet Meal
11Sequence
- 1. Open freezer door
- 2. Take out Gourmet Meal
- 3. Close freezer door
- 4. Open microwave door
- 5. Put Gourmet Meal on carousel
- 6. Shut microwave door
- 7. Set microwave on high for 5 minutes
- 8. Start microwave
- 9. Wait 5 minutes
- 10. Open microwave door
- 11. Remove Gourmet Meal
- 12. Close microwave door
12Procedure
- A named sequence
- Refer to it collectively (by name)
- ...instead of individually (by each instruction
in sequence) - Example
- Driving to Uni
13Procedure
...etc...etc...etc... 52. find
parking space 53. pull into parking space
54. turn off engine 55. remove keys from
ignition 56. open car door 57. get out
58. shut car door 59. lock car door 60.
enable alarm
- procedure DriveToUni
-
- 1. find car keys
- 2. disable car alarm
- 3. open car door
- 4. get in car
- 5. shut car door
- 6. put keys in ignition
- 7. start car
- 8. back car out of driveway
- 9. drive to end of street
- 10. turn right
- 11. drive to end of street
- 12. turn left
- ...etc...etc...etc
-
14Selection
- Choose between two courses of action
- That is, between two instructions (or sequences
or procedures) - Decision based on a single true/false question
- Examples
- Car repairs
- Reciprocals
- Before driving to uni
15Selection
- if (motor turns)
- then
- CheckFuel
- CheckSparkPlugs
- CheckCarburettor
-
- else
- CheckDistributer
- CheckIgnitionCoil
-
16Selection
- input Number
- if (Number ? 0)
- then
- output 1/Number
-
- else
- output "infinity"
-
17Selection
18Repetition
- Repeat an instruction...
- ...while (or maybe until) some condition occurs
- Test the condition each time before repeating the
instruction - Also known as iteration
- Example
- Getting a date
19Repetition
- procedure AskOnDate(name, time, location)
-
- Phone(name)
- Say("Hey", name, "it's your lucky day!")
- Say("Wanna come to", location, "at", time,
"?")ListenToReplystart begging count at
zerowhile (reply is "No" and begging count lt
100) Say("Oh please!") - add 1 to begging count
20Repetition
- decide on time and location
- until (successfully booked)
-
- get next name in little black book
- AskOnDate(name, time, location)
-
- SighWithRelief
21Documentation
- Records what the algorithm does
- Describes how it does it
- Explains the purpose of each component of the
algorithm - Notes restrictions or expectations
- Example
- Getting a date (again)
22Documentation
- Thing of something romantic to do (e.g. footy,
video games, chess) - decide on time and location
- Work through address book looking for
someonewho's forgotten what a lousy date I am - until (successfully booked)
-
- get next name in little black book
- AskOnDate(name, time, location)
-
- Will only reach here if someone's memory (or
taste) is bad - SighWithRelief
23The Software Development Process
- Define the problem clearly
- Analyse the problem thoroughly
- Design an algorithm carefully
- Code the algorithm efficiently
- Test the code thoroughly
- Document the system lucidly
24Designing an algorithm
- Top-down
- Bottom-up
- Object-oriented
- Prototyping
- Evolutionary
25Top-down design
- Write down what you have to do
- Break that into 3-7 smaller steps
- Break each step into 3-7 smaller steps
- Keeping subdividing until each individual step is
easy enough to do - That is, until it's a single instruction
- Example
- Learning
26Top-down design
Learn
27The Game of Life
- Invented by mathematician John Conway (no
relation) - As seen on millions of screen savers around the
world - Now in the comfort of your own lecture theatre...
28The Game of Life
- stand up if your age is even
- while (sign says GO) count how many people
next to you are standing - if (exactly 3 of your neighbours are
standing) raise your hand - otherwise if (exactly 2 of your neighbours are
standing AND you are standing
too) raise your hand - wait till the sign says CHANGE
- wait till the sign doesn't say CHANGE
- if (your hand is up) stand up
otherwise sit down put your
hand down
29The Game of Life
GO
stand up if your age is even while (sign says
GO) count how many people next to you are
standing if (exactly 3 of your neighbours are
standing) raise your hand
otherwise if (exactly 2 of your neighbours are
standing AND you are standing
too) raise your hand wait till
the sign says CHANGE wait till the sign
doesn't say CHANGE if (your hand is up)
stand up otherwise sit down
put your hand down
30The Game of Life
CHANGE
stand up if your age is even while (sign says
GO) count how many people next to you are
standing if (exactly 3 of your neighbours are
standing) raise your hand
otherwise if (exactly 2 of your neighbours are
standing AND you are standing
too) raise your hand wait till
the sign says CHANGE wait till the sign
doesn't say CHANGE if (your hand is up)
stand up otherwise sit down
put your hand down
31The Game of Life
STOP
stand up if your age is even while (sign says
GO) count how many people next to you are
standing if (exactly 3 of your neighbours are
standing) raise your hand
otherwise if (exactly 2 of your neighbours are
standing AND you are standing
too) raise your hand wait till
the sign says CHANGE wait till the sign
doesn't say CHANGE if (your hand is up)
stand up otherwise sit down
put your hand down
32Reading
- Deitel Deitel, C How to program
- Chapter 3, Sections 3.8 to 3.13