structured problem solving

62
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]

Upload: arin

Post on 15-Feb-2016

58 views

Category:

Documents


0 download

DESCRIPTION

Structured Problem Solving. Week 5: Steps in Problem Solving Stewart Blakeway FML 213 [email protected]. 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 - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

Structured Problem Solving

Week 5: Steps in Problem SolvingStewart BlakewayFML [email protected] Faculty of Sciences and Social Sciences HOPE1What we have done alreadySeen what an algorithm isa set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms inStructured EnglishFlow chartsUsed variables to remember2www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE2What we shall do todayStart writing out own algorithmsPages 36 to 503www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE3Steps in Problem SolvingStep 1 Establish the overall plan of actionwrite down the solution as a single statement

Step 2 Examine the statement to see if it could be expressed as one of the following:SequenceSelectionRepetition

4www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE4Steps in Problem SolvingStep 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 detail5www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE5SummaryStep 1 Establish the overall plan of actionwrite down the solution as a single statement

Step 2 Examine the statement to see if it could be expressed as one of the following:SequenceSelectionRepetition

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 detail6www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE6Technical Phrases for this TechniqueTop Down DesignStepwise Refinement

7www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE7Making sure we understand what the problem is we are trying to solveThis conforms to obeying the first Law of ComputingMaking sure we get paidIf we build what client wants then it is more likely we shall get paid8www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE8Making sure we understand what the problem is we are trying to solveSome Thoughts on Building a Dwelling9www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE9Some Thoughts on Building a DwellingInitial ideasType of HouseSize of HouseLocation

Now analyse each of the above

10www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE10Type of HouseBungalowTwo FloorsThree FloorsDetachedTerracedSemi-detached11www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE11Size of HouseHow many bedrooms ?How many living rooms ?How many bathrooms ?12www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE12LocationHow many miles from shops ?Green belt ?North South facing ?

13www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE13Repeat this analysis untilThe level of detail is complete and unambiguousFront door exterior: Oxford blue glossMain bathroom taps: gold platedAnd so-forth14www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE14An Example of Using Stepwise RefinementEggs 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. 15www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE15SummaryStep 1 Establish the overall plan of actionwrite down the solution as a single statement

Step 2 Examine the statement to see if it could be expressed as one of the following:SequenceSelectionRepetition

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 detail16www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE16Step 1: Write down the solution as a single statementDeal with eggs

17www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE17Step 2: Examine the statement to see if it could be expressed as sequence, selection or iterationDeal with eggs

Sequence ?Selection ?Repetition ?18Is it a sequence of substeps?Is it a choice between alternatives?Is it a repeated action?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE18Step 2: Examine the statement to see if it could be expressed as sequence, selection or iterationDeal with eggs

Sequence

19www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE19Step 3: Make it so a sequence20Put tray on tableProcess eggsReport number cracked

First Refinementwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE20Step 4: Repeat steps 2 and 3 for the new statements this generates21www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE211 Put Tray on Table22No further refinement necessarywww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE22 2 Process the Eggs23Treat this as a problem in its own right.Is there a repetition ? www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE23 2 Process the Eggs24Treat this as a problem in its own right.Is there a repetition ? YES !When does it stop ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE24 2 Process the Eggs25Treat this as a problem in its own right.Is there a repetition ? YES !When does it stop ?When he has processedall the eggs !What does that mean ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE25 2 Process the Eggs26Treat this as a problem in its own right.Is there a repetition ? YES !When does it stop ?When he has processedall the eggs !What does that mean ?When there are no moreeggs to processwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE26When There Are No More Eggs to Process27How can we rewrite this using the problem solving construct:while something is truebegindo somethingendwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE27while something is true28When 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 !www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE28The repetition so far 29while eggs remain to be examinedbegindo somethingendwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE29begin do somethingend30do somethingKeep it Simpledeal with eggNot eggswww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE303 Report Number Cracked31No further refinement yet.www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE31Second RefinementSolution So Far32put tray on tablewhile eggs remain to be examinedbegin deal with eggendreport number cracked

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE32deal with egg33Revisit the problem:How do we deal with egg ?Is it a sequence ?Yes !Is it a repetition ?No !Is it a selection ?Not Yet !www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE33deal with egg34Sequence:pick up eggdeal with cracked eggwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE34pick up egg35End of story: level of detail fine: no further refinement needed.www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE35deal with cracked egg36Revisit 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?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE36if condition then..else37if egg is cracked thenbegin put it in blue boxadd 1 to total of cracked eggsendelsebegin return egg to trayendwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE37Solution So Far38put tray on tablewhile eggs remain to be examinedbegin pick up eggif egg is cracked thenbeginput it in blue boxadd 1 to total of cracked eggsend else begin return egg to trayend end report number cracked

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE38Third Refinement39Let us use a variable to store the number of cracked eggs.Convention:Use a name which reflects the purpose of the variableStart with a capital letterNo spacesIf words are joined together make the first letter of each word a capital letterSo in this case: NumberCrackedwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE39NumberCracked40Just 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 ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE40Initialising the Variable41NumberCracked := 0www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE41add 1 to total of cracked eggs42How ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE4243NumberCracked := NumberCracked +1www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE43The Solution

Final refinement44www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE4445NumberCracked := 0put tray on tablewhile eggs remain to be examinedbegin pick up eggif egg is cracked thenbeginput it in blue boxNumberCracked := NumberCracked + 1end else begin return egg to trayend end report NumberCrackedwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE45A Second Example of Using Stepwise Refinment46A 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.

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE46Step 147Deal with parcels

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE47Step 248Deal with parcelsSequence ?Selection ?Repetition ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE48Step 249Deal with parcelsSequencewww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE49Step 3: Sequence - Make it so50Deal with parcels on conveyor beltSend containers to the despatch office

First Refinementwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE501. Deal with parcels on conveyor belt51Revisit the problem:Is there repetition ?Yes !When does it stop ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE511. Deal with parcels on conveyor belt52Revisit the problem:Is there repetition ?Yes !When does it stop ?When the conveyor beltstops.What is the while test ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE521. Deal with parcels on conveyor belt53Revisit the problem:Is there repetition ?Yes !When does it stop ?When the conveyor belt stops.What is the while test ?The opposite of thisConveyor belt runningwww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE532. Send containers to despatch office54No further refinement needed .www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE54Second Refinement55while conveyor belt runningbegindeal with parcelend send containers to the despatch officewww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE55deal with parcel56Sequence ?Yes !Selection ?Not Yet !Repetition ?No !www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE56deal with parcel57Revisit the problem:Sequence: take parcel off conveyor beltweigh parcel stamp parcel with weightput in correct box

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE57Third refinement58while conveyor belt runningbegin take parcel off conveyor beltweigh parcel stamp parcel with weightput in correct boxend send containers to despatch office

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE58Lets go Further59take parcel off conveyor belt:no more detail neededweigh parcel :no more detail neededstamp parcel with weight:no more detail neededput in correct box:selection here !

Which box ?www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE59Which Box ?60Revisit the Problem:if light parcelblue boxif medium parcelred boxif heavy parcelgreen box

www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE60The Final Algorithm61www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE6162while conveyor belt runningbegintake parcel off conveyor beltweigh parcelstamp parcel with weightif parcel is light thenbegin put in blue boxendif parcel is medium thenbeginput in red boxendif parcel is heavy thenbegin put in green boxendendsend containers to despatch officewww.hope.ac.uk Faculty of Sciences and Social Sciences HOPE62