www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213 [email protected]
Transcript
Slide 1
Slide 2
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Structured Problem Solving Week 5: Steps in Problem Solving Stewart
Blakeway FML 213 [email protected]
Slide 3
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
What we have done already Seen what an algorithm is a set of
instructions that, if carried out, will lead to a successful
conclusion Learned how to represent algorithms in Structured
English Flow charts Used variables to remember 2
Slide 4
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
What we shall do today Start writing out own algorithms Pages 36 to
50 3
Slide 5
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Steps in Problem Solving Step 1 Establish the overall plan of
action write down the solution as a single statement Step 2 Examine
the statement to see if it could be expressed as one of the
following: Sequence Selection Repetition 4
Slide 6
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Steps in Problem Solving Step 3 Make it so. write it out as one of
those Step 4 Repeat steps 2 and 3 for the new statements this
generates Step 5 Stop when you are satisfied with the level of
detail 5
Slide 7
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Summary Step 1 Establish the overall plan of action write down the
solution as a single statement Step 2 Examine the statement to see
if it could be expressed as one of the following: Sequence
Selection Repetition Step 3 Make it so. write it out as one of
those Step 4 Repeat steps 2 and 3 for the new statements this
generates Step 5 Stop when you are satisfied with the level of
detail 6
Slide 8
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Technical Phrases for this Technique Top Down Design Stepwise
Refinement 7
Slide 9
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Making sure we understand what the problem is we are trying to
solve This conforms to obeying the first Law of Computing Making
sure we get paid If we build what client wants then it is more
likely we shall get paid 8
Slide 10
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Making sure we understand what the problem is we are trying to
solve Some Thoughts on Building a Dwelling 9
Slide 11
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Some Thoughts on Building a Dwelling Initial ideas Type of House
Size of House Location Now analyse each of the above 10
Slide 12
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Type of House Bungalow Two Floors Three Floors Detached Terraced
Semi-detached 11
Slide 13
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Size of House How many bedrooms ? How many living rooms ? How many
bathrooms ? 12
Slide 14
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Location How many miles from shops ? Green belt ? North South
facing ? 13
Slide 15
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Repeat this analysis until The level of detail is complete and
unambiguous Front door exterior: Oxford blue gloss Main bathroom
taps: gold plated And so-forth 14
Slide 16
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE An
Example of Using Stepwise Refinement Eggs are stored on trays in a
warehouse. The egg checker puts a tray on his work table. He then
picks up each egg in turn. If it is cracked, he puts it into the
blue box, otherwise he returns it to the tray. As he works through
the eggs, he keeps count of the number cracked. When he has
finished the tray, he reports how many eggs were cracked. 15
Slide 17
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Summary Step 1 Establish the overall plan of action write down the
solution as a single statement Step 2 Examine the statement to see
if it could be expressed as one of the following: Sequence
Selection Repetition Step 3 Make it so. write it out as one of
those Step 4 Repeat steps 2 and 3 for the new statements this
generates Step 5 Stop when you are satisfied with the level of
detail 16
Slide 18
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 1: Write down the solution as a single statement Deal with
eggs 17
Slide 19
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 2: Examine the statement to see if it could be expressed as
sequence, selection or iteration Deal with eggs Sequence ?
Selection ? Repetition ? 18 Is it a sequence of substeps? Is it a
choice between alternatives? Is it a repeated action?
Slide 20
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 2: Examine the statement to see if it could be expressed as
sequence, selection or iteration Deal with eggs Sequence 19
Slide 21
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 3: Make it so a sequence 20 1.Put tray on table 2.Process eggs
3.Report number cracked First Refinement
Slide 22
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 4: Repeat steps 2 and 3 for the new statements this generates
21
Slide 23
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1
Put Tray on Table 22 No further refinement necessary
Slide 24
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2
Process the Eggs 23 Treat this as a problem in its own right. Is
there a repetition ?
Slide 25
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2
Process the Eggs 24 Treat this as a problem in its own right. Is
there a repetition ? YES ! When does it stop ?
Slide 26
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2
Process the Eggs 25 Treat this as a problem in its own right. Is
there a repetition ? YES ! When does it stop ?When he has processed
all the eggs ! What does that mean ?
Slide 27
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2
Process the Eggs 26 Treat this as a problem in its own right. Is
there a repetition ? YES ! When does it stop ?When he has processed
all the eggs ! What does that mean ?When there are no more eggs to
process
Slide 28
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
When There Are No More Eggs to Process 27 How can we rewrite this
using the problem solving construct: while something is true begin
do something end
Slide 29
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
while something is true 28 When do you stop ? When all the eggs
have been examined ! What is the opposite of this ? Eggs remain to
be examined ! This is the while test !
Slide 30
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The
repetition so far 29 while eggs remain to be examined begin do
something end
Slide 31
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
begin do something end 30 do something Keep it Simple deal with egg
Not eggs
Slide 32
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 3
Report Number Cracked 31 No further refinement yet.
Slide 33
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Second Refinement Solution So Far 32 put tray on table while eggs
remain to be examined begin deal with egg end report number
cracked
Slide 34
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
deal with egg 33 Revisit the problem: How do we deal with egg ? Is
it a sequence ?Yes ! Is it a repetition ?No ! Is it a selection
?Not Yet !
Slide 35
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
deal with egg 34 Sequence: pick up egg deal with cracked egg
Slide 36
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
pick up egg 35 End of story: level of detail fine: no further
refinement needed.
Slide 37
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
deal with cracked egg 36 Revisit the problem: He then picks up each
egg in turn. If it is cracked, he puts it into the blue box,
otherwise he returns it to the tray. As he works through the eggs,
he keeps count of the number cracked. Here is the selection
mentioned earlier (the not yet ). Can we express this in the
format: if condition then actions else other actions ?
Slide 38
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE if
condition then..else 37 if egg is cracked then begin put it in blue
box add 1 to total of cracked eggs end else begin return egg to
tray end
Slide 39
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Solution So Far 38 put tray on table while eggs remain to be
examined begin pick up egg if egg is cracked then begin put it in
blue box add 1 to total of cracked eggs end else begin return egg
to tray end report number cracked
Slide 40
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Third Refinement 39 Let us use a variable to store the number of
cracked eggs. Convention: Use a name which reflects the purpose of
the variable Start with a capital letter No spaces If words are
joined together make the first letter of each word a capital letter
So in this case: NumberCracked
Slide 41
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
NumberCracked 40 Just as we would reset the odometer in our car to
avoid doing mind boggling subtractions to find the length of a
journey, we must set the initial value of NumberCracked to an
appropriate value. Otherwise the final count could be anything.
This is called initialisation and in this instance we would
initialise the variable to ???????????? What ? How ?
Slide 42
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Initialising the Variable 41 NumberCracked := 0
Slide 43
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE add
1 to total of cracked eggs 42 How ?
Slide 44
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 43
NumberCracked := NumberCracked +1
Slide 45
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The
Solution Final refinement 44
Slide 46
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 45
NumberCracked := 0 put tray on table while eggs remain to be
examined begin pick up egg if egg is cracked then begin put it in
blue box NumberCracked := NumberCracked + 1 end else begin return
egg to tray end report NumberCracked
Slide 47
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A
Second Example of Using Stepwise Refinment 46 A parcel sorting
office initially sorts the parcels by weight into three broad
categories: light, medium and heavy. The parcels enter the weighing
room on a circular conveyor belt, where they are taken off,
weighed, weight stamped and placed in blue, red or green containers
depending on whether they are light, medium or heavy respectively.
When the conveyor belt stops, the containers are sent to the
despatch office.
Slide 48
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 1 47 Deal with parcels
Slide 49
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 2 48 Deal with parcels Sequence ? Selection ? Repetition
?
Slide 50
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 2 49 Deal with parcels Sequence
Slide 51
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Step 3: Sequence - Make it so 50 1.Deal with parcels on conveyor
belt 2.Send containers to the despatch office First Refinement
Slide 52
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1.
Deal with parcels on conveyor belt 51 Revisit the problem: Is there
repetition ?Yes ! When does it stop ?
Slide 53
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1.
Deal with parcels on conveyor belt 52 Revisit the problem: Is there
repetition ?Yes ! When does it stop ?When the conveyor belt stops.
What is the while test ?
Slide 54
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1.
Deal with parcels on conveyor belt 53 Revisit the problem: Is there
repetition ?Yes ! When does it stop ?When the conveyor belt stops.
What is the while test ?The opposite of this Conveyor belt
running
Slide 55
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2.
Send containers to despatch office 54 No further refinement
needed.
Slide 56
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Second Refinement 55 while conveyor belt running begin deal with
parcel end send containers to the despatch office
Slide 57
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
deal with parcel 56 Sequence ?Yes ! Selection ?Not Yet ! Repetition
?No !
Slide 58
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
deal with parcel 57 Revisit the problem: Sequence: take parcel off
conveyor belt weigh parcel stamp parcel with weight put in correct
box
Slide 59
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Third refinement 58 while conveyor belt running begin take parcel
off conveyor belt weigh parcel stamp parcel with weight put in
correct box end send containers to despatch office
Slide 60
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Lets go Further 59 take parcel off conveyor belt:no more detail
needed weigh parcel :no more detail needed stamp parcel with
weight:no more detail needed put in correct box:selection here !
Which box ?
Slide 61
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE
Which Box ? 60 Revisit the Problem: if light parcelblue box if
medium parcelred box if heavy parcelgreen box
Slide 62
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The
Final Algorithm 61
Slide 63
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 62
while conveyor belt running begin take parcel off conveyor belt
weigh parcel stamp parcel with weight if parcel is light then begin
put in blue box end if parcel is medium then begin put in red box
end if parcel is heavy then begin put in green box end send
containers to despatch office