630 likes | 953 Views
Structured Problem Solving. Week 5: Steps in Problem Solving Stewart Blakeway FML 213 blakews@hope.ac.uk. 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
E N D
Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213 blakews@hope.ac.uk
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
What we shall do today • Start writing out own algorithms • Pages 36 to 50
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
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
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
Technical Phrases for this Technique • Top Down Design • Stepwise Refinement
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
Making sure we understand what the problem is we are trying to solve • Some Thoughts on Building a Dwelling
Some Thoughts on Building a Dwelling • Initial ideas • Type of House • Size of House • Location • Now analyse each of the above
Type of House • Bungalow • Two Floors • Three Floors • Detached • Terraced • Semi-detached
Size of House • How many bedrooms ? • How many living rooms ? • How many bathrooms ?
Location • How many miles from shops ? • Green belt ? • North – South facing ?
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
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.
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
Step 1: Write down the solution as a single statement • Deal with eggs
Step 2: Examine the statement to see if it could be expressed as sequence, selection or iteration • Deal with eggs • Sequence ? • Selection ? • Repetition ? Is it a sequence of substeps? Is it a choice between alternatives? Is it a repeated action?
Step 2: Examine the statement to see if it could be expressed as sequence, selection or iteration • Deal with eggs • Sequence
Step 3: Make it so – a sequence • Put tray on table • Process eggs • Report number cracked • First Refinement
Step 4: Repeat steps 2 and 3 for the new statements this generates
1 Put Tray on Table No further refinement necessary
2 Process the Eggs Treat this as a problem in its own right. Is there a repetition ?
2 Process the Eggs Treat this as a problem in its own right. Is there a repetition ? YES ! When does it stop ?
2 Process the Eggs 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 ?
2 Process the Eggs 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
When There Are No More Eggs to Process • How can we rewrite this using the problem solving construct: • while something is true • begin • do something • end
while something is true 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 !
The repetition so far … • while eggs remain to be examined • begin • do something • end
begin do somethingend do something Keep it Simple deal with egg Not eggs
3 Report Number Cracked No further refinement yet.
Second RefinementSolution So Far • put tray on table • while eggs remain to be examined • begin • deal with egg • end • report number cracked
deal with egg 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 !
deal with egg • Sequence: • pick up egg • deal with cracked egg
pick up egg End of story: level of detail fine: no further refinement needed.
deal with cracked egg 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 (thenot yet ). Can we express this in the format: if condition then actions else other actions ?
ifcondition then…..else • 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
Solution So Far • 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 • end • report number cracked
Third Refinement • 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
NumberCracked 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 ?
Initialising the Variable NumberCracked := 0
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 • end • report NumberCracked
A Second Example of Using Stepwise Refinment 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.
Step 1 • Deal with parcels
Step 2 • Deal with parcels • Sequence ? • Selection ? • Repetition ?
Step 2 • Deal with parcels • Sequence
Step 3: Sequence - Make it so • Deal with parcels on conveyor belt • Send containers to the despatch office • First Refinement