1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI...
-
Upload
candice-shepherd -
Category
Documents
-
view
215 -
download
1
Transcript of 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI...
![Page 2: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/2.jpg)
2
![Page 3: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/3.jpg)
3
![Page 4: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/4.jpg)
4
![Page 5: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/5.jpg)
5
![Page 6: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/6.jpg)
6
![Page 7: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/7.jpg)
7
![Page 8: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/8.jpg)
8
![Page 9: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/9.jpg)
9
Making a tea
![Page 10: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/10.jpg)
10
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)
![Page 11: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/11.jpg)
11
Variables
This jarcan contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
ValuesVariable
• Are containers for values – places to store values
• Example:
![Page 12: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/12.jpg)
12
Restrictions on Variables
• Variables may be restricted to contain a specific type of value
![Page 13: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/13.jpg)
13
Instructions (Primitives)
• Some action that is – simple– unambiguous– that the system knows about...– ...and should be able to actually do
![Page 14: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/14.jpg)
14
Instructions -- Application
• Some instructions can only be applied to a specific type of values or variables
• Examples:
![Page 15: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/15.jpg)
15
Sequence
• A series of instructions
• ...to be carried out one after the other...
• ...without hesitation or question
![Page 16: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/16.jpg)
16
Procedure
• A named sequence of instructions
• So that you can– Refer to it collectively (by name)– ...instead of individually (by each instruction in
the sequence)
• Example:
– Drive_To_Uni
![Page 17: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/17.jpg)
17
Procedure -- Example procedure Drive_To_Uni { 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
...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
}
![Page 18: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/18.jpg)
18
Procedure – Example (cont)
procedure Do_Wednesday{ Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture ...etc...etc...etc... Drive_From_Uni ...etc...etc...etc...}
procedure Do_Week
{
Do_Monday
Do_Tuesday
Do_Wednesday
Do_Thursday
...etc...etc...etc...
}
![Page 19: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/19.jpg)
19
Procedure – Example (cont)
procedure Do_Wednesday{ Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture ...etc...etc...etc... Drive_From_Uni ...etc...etc...etc...}
In this subject, we also use the following words to refer to a “Procedure” :
• Sub-routine• Module• Function
![Page 20: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/20.jpg)
20
Procedure – Example (cont)
procedure Do_Wednesday{ Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture ...etc...etc...etc... Drive_From_Uni ...etc...etc...etc...}
We use brackets to mark the beginning and end of a sequence.
![Page 21: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/21.jpg)
21
Procedure – Example (cont)
procedure Do_Wednesday{ Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture ...etc...etc...etc... Drive_From_Uni ...etc...etc...etc...}
An instruction invoking a procedure is known as a “procedure call”
![Page 22: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/22.jpg)
22
Procedure• A procedure may have a set of parameters
procedure customerService ( myName ,timeOfDay ){ say “Good timeOfDay” say “My name is myName” say “How can I help you?”}
customerService ( “Ann”, “Morning” )
customerService (“Ann”, “Afternoon” )
customerService ( “Jeff”, “Afternoon” )
![Page 23: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/23.jpg)
23
Selection• An instruction that decides which of two
possible sequences is executed
• The decision is based on a single true/false condition
• Example:
– Car repair
![Page 24: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/24.jpg)
24
Selection Example -- Car Repairif (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
![Page 25: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/25.jpg)
25
Selection Example –Car Repair (cont)
if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Should be a true or false condition.
![Page 26: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/26.jpg)
26
Selection Example --Car Repair (cont)
if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Sequence if the condition is true.
![Page 27: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/27.jpg)
27
Selection Example --Car Repair (cont)
if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Sequence if the condition is false.
![Page 28: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/28.jpg)
28
Selection -- Exercise
input Num if (Num is not equal 0) then { output 1/Num } else {
output "infinity" }
Will the following algorithms produce the same output?
Algorithm 1: input Num
if (Num is not equal 0)
then
{
output 1/Num
}
output "infinity"
Algorithm 2:
![Page 29: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/29.jpg)
29
Selection – Several Conditions
• What if several conditions need to be satisfied?if ( today is Wednesday and the time is 10.00am )
then
{
Go to CSE1301 Lecture
}
else
{
Go to Library
} Solution 1
![Page 30: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/30.jpg)
30
Selection – Several Conditions (cont)
Solution 2
Often called a “nested selection”
if ( today is Wednesday ) then {
if ( the time is 10.00am ) then { Go to CSE1301 Lecture } } else ...etc...etc...etc...
![Page 31: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/31.jpg)
31
Selection – At Least One of Several Conditions
• What if at least one of several conditions needs to be satisfied?if ( I feel hungry or the time is 1.00pm or my
mate has his eye on my lunch )
then
{
Eat my lunch now
}
![Page 32: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/32.jpg)
32
Repetition
• Repeat an instruction...– ...while (or maybe until) some true or
false condition occurs– Test the condition each time before
repeating the instruction• Also known as iteration or loop• Example:
– Algorithm for getting a date
![Page 33: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/33.jpg)
33
Repetition -- Exampleprocedure AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!")
add 1 to begging count
ListenToReply ( )}
}
![Page 34: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/34.jpg)
34
Repetition – Example (cont)procedure AskOnDate ( name, time, location ){
Phone(name)Say("Hey", name, "it's your lucky day!")Say("Wanna come to", location, "at", time, "?")ListenToReply ( )start begging count at zerowhile ( reply is "No" and begging count < 100 ){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Condition is tested before sequence
![Page 35: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/35.jpg)
35
Repetition – Example (cont)procedure AskOnDate ( name, time, location ){
Phone(name)Say("Hey", name, "it's your lucky day!")Say("Wanna come to", location, "at", time, "?")ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Ensure initial values of variables used in the conditions are set correctly
![Page 36: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/36.jpg)
36
Repetition – Example (cont)
procedure AskOnDate ( name, time, location ){
Phone(name)Say("Hey", name, "it's your lucky day!")Say("Wanna come to", location, "at", time, "?")ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Ensure the variables used in the conditions are updated in each iteration
![Page 37: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/37.jpg)
37
Repetition – Example (cont)
procedure AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!")
}
}Infinite loop
• What if we don’t increment the begging count?
![Page 38: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/38.jpg)
38
Documentation
• Records what the algorithm does
• Describes how it does it
• Explains the purpose of each component of the algorithm
• Notes restrictions or expectations
![Page 39: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/39.jpg)
39
The 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
![Page 40: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/40.jpg)
40
![Page 41: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/41.jpg)
41
Family Tree of
Programming
Languages
![Page 42: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/42.jpg)
42
Our Flow Chart
Start-stop
instruction blocksif
forwhile
readwrite
STARTEND
![Page 43: 1 BBG2 Algorithms Components of an Algorithm Instructor: M.Fatih AMASYALI E-mail:mfatih@ce.yildiz.edu.tr.](https://reader030.fdocuments.in/reader030/viewer/2022032709/56649ed95503460f94be8505/html5/thumbnails/43.jpg)
43
Referance
• http://gmm.fsksm.utm.my/~dzul/NoteC/