1 / 62

Structured Problem Solving

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

arin
Download Presentation

Structured Problem Solving

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213 blakews@hope.ac.uk

  2. 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

  3. What we shall do today • Start writing out own algorithms • Pages 36 to 50

  4. 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

  5. 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

  6. 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

  7. Technical Phrases for this Technique • Top Down Design • Stepwise Refinement

  8. 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

  9. Making sure we understand what the problem is we are trying to solve • Some Thoughts on Building a Dwelling

  10. Some Thoughts on Building a Dwelling • Initial ideas • Type of House • Size of House • Location • Now analyse each of the above

  11. Type of House • Bungalow • Two Floors • Three Floors • Detached • Terraced • Semi-detached

  12. Size of House • How many bedrooms ? • How many living rooms ? • How many bathrooms ?

  13. Location • How many miles from shops ? • Green belt ? • North – South facing ?

  14. 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

  15. 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.

  16. 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

  17. Step 1: Write down the solution as a single statement • Deal with eggs

  18. 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?

  19. Step 2: Examine the statement to see if it could be expressed as sequence, selection or iteration • Deal with eggs • Sequence

  20. Step 3: Make it so – a sequence • Put tray on table • Process eggs • Report number cracked • First Refinement

  21. Step 4: Repeat steps 2 and 3 for the new statements this generates

  22. 1 Put Tray on Table No further refinement necessary

  23. 2 Process the Eggs Treat this as a problem in its own right. Is there a repetition ?

  24. 2 Process the Eggs Treat this as a problem in its own right. Is there a repetition ? YES ! When does it stop ?

  25. 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 ?

  26. 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

  27. 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

  28. 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 !

  29. The repetition so far … • while eggs remain to be examined • begin • do something • end

  30. begin do somethingend do something Keep it Simple deal with egg Not eggs

  31. 3 Report Number Cracked No further refinement yet.

  32. Second RefinementSolution So Far • put tray on table • while eggs remain to be examined • begin • deal with egg • end • report number cracked

  33. 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 !

  34. deal with egg • Sequence: • pick up egg • deal with cracked egg

  35. pick up egg End of story: level of detail fine: no further refinement needed.

  36. 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 ?

  37. 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

  38. 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

  39. 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

  40. 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 ?

  41. Initialising the Variable NumberCracked := 0

  42. add 1 to total of cracked eggs How ?

  43. NumberCracked := NumberCracked +1

  44. The SolutionFinal refinement

  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 • end • report NumberCracked

  46. 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.

  47. Step 1 • Deal with parcels

  48. Step 2 • Deal with parcels • Sequence ? • Selection ? • Repetition ?

  49. Step 2 • Deal with parcels • Sequence

  50. Step 3: Sequence - Make it so • Deal with parcels on conveyor belt • Send containers to the despatch office • First Refinement

More Related