1 / 26

Problem Solving for Programming

Problem Solving for Programming. Session 3 Dealing with Sequence, Choice and Repetition. Sequence. Consider the coffee problem from the exercises in Vickers Chapter 3.

gilles
Télécharger la présentation

Problem Solving for Programming

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. Problem Solving for Programming Session 3Dealing with Sequence, Choice and Repetition

  2. Sequence • Consider the coffee problem from the exercises in Vickers Chapter 3. • What are the steps involved in making a pot of coffee and pouring a cup using a percolator similar to the one above (no more than three steps)? Water reservoir Using an electronic filter machine (also called a percolator) make a pot of coffee and pour a cup. Swing out holder for the filter On/off switch Coffee jug

  3. Sequence • There are different possible sequences, but only one guarantees you won’t damage the percolator. • The first solution is correct, but it is an outline solution only; not a fully fleshed out algorithm. To develop a useful algorithm for the coffee making problem, we need a lower level of abstraction. We need to fill in the detail for each step. • To do this, we need to employ the problem solving framework we outlined in session 1: (1) Understand the problem (2) Devise a plan to solve the problem (3) Carry out the plan (4) Assess the result (5) Describe what has been learned from the process (6) Document the solution. • Turn on the machine ; • Put water and coffee in the machine ; • Pour coffee ; • Put water and coffee in the machine ; • Turn on the machine ; • Pour coffee ;

  4. Understanding the problem Using an electronic filter machine (percolator), make a pot of coffee and pour a cup. • Q. What are you being asked to do? • To make a pot of coffee and pour a cup. • Q. What is required? • A cup of coffee. • Q. Is that all that is required? • Before we can get a cup of coffee, we need to add water and coffee to make enough coffee to pour into the cup. • Q. What are the unknowns? • How much coffee to make. We need to pour a cup, but we also have to make a pot. How much is a pot? Does it mean a whole pot, or just enough coffee to fill one cup? Are we making black coffee or white? Sweetened or unsweetened? How does the machine work? Does it require filter papers?

  5. Understanding the problem • Q. Can the problem be better expressed by drawing a diagram or a picture? • The problem is not so difficult to envisage, so a drawing is probably unnecessary. • Q. What are the principal parts of the problem 1) Make a pot of coffee. 2) Pour a cup. Or, looking at it another way: 1) The filter machine, 2) the cup, 3) the coffee, 4) some water, 5) the cup of coffee. • Q. Have you made any assumptions? • We have assumed that the jug holds more than one cupful of coffee, which is not always the case — it is possible to buy single-cup filter machines. We have also assumed black coffee is required. • Q. What can you do about the assumptions? • We could ask the person who wants us to make the coffee what size coffee machine to use.

  6. Devising a plan • Q. Have you solved this problem before/is it similar to one you have solved before? • Not exactly, but most of us have used percolators. • Q. Are some parts of the problem more easily solved than others? • Yes. Pouring the coffee is easy. Adding the water and the coffee grounds requires some thought as to how much to add. After pouring the coffee we do not know if anything else is needed (e.g. milk and sugar). • Q. Does restating the problem help? Try restating it in a different language of description. • Probably not applicable. This is not a complex logical problem, or one that requires a table or graphic to be solved.

  7. Devising a plan • Q. Did you make use of all the information in the problem statement? • The statement seems to be incomplete. We still do not know how much coffee to make or whether milk and sugar are needed. • Q. Can you satisfy all the conditions of the problem? • If we make some assumptions, yes. Without any further information. We have to assume that: • making a coffee means making enough for one cup. • the machine requires a filter. • black coffee is required. • we have to turn off the machine after we have finished making the coffee.

  8. Carrying out the plan • Now we can satisfy all the conditions of the problem, and we are aware of the assumptions we have made, we can write out the basic sequence of actions necessary to solve the problem. • Write the actions for making a cup of coffee using the pseudo code notation (approximately 10 actions).

  9. Carrying out the plan • A basic sequence of actions for this problem may look something like this (assumptions underlined). 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. Pour coffee into cup ; 10. Turn off machine ;

  10. Making choices • Now imagine that the original problem has been extended thus:Using an electronic coffee machine (percolator), make a pot of coffee and pour a cup. Add milk and sugar as required. • Would the solution we previously devised suffice? • Obviously not. It does not account for the requirement to add sugar and milk as required. • We would have to adapt our original algorithm to satisfy this requirement. • How could we do this?

  11. Making choices 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. Add sugar ; 10. Add milk/cream ; 11. Pour coffee into cup ; 12. Stir coffee ; 13. Turn off machine ; • This solution appears to work. But there is one problem. What happens if the person we are making coffee for does not want milk or sugar? • In order to solve this problem, we need to introduce some element of choice/selection into our algorithm. • If the person wants milk and/or sugar, we will add it. If he/she does not want milk and/or sugar, we will not add it.

  12. Choice • In algorithms, choice is expressed using an IF . . ENDIF statement. IF (fuel remaining less than 10%) Display fuel warning light ; ENDIF If (raining) Take umbrella ENDIF • Write IF . . ENDIF statements for adding milk and sugar in the coffee making problem.

  13. Choice 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. IF (sugar required) 9.1 Add sugar ; ENDIF 10.IF (white coffee required) 10.1 Add milk ; ENDIF 11.Pour coffee into cup ; 12.Stir coffee ; 13.Turn off machine ; • The solution now allows us to offer a choice and our guests are not forced to drink black, unsweetened coffee. • However, the solution as it stands only allows us to add one sugar. What if more than one sugar is required? How could we handle this?

  14. Repetition. • If two sugars are required, we could try the following: IF (sugar required) Add sugar ; ENDIF IF (more sugar required) Add sugar ; ENDIF . . . . . • But what if we have someone with a really sweet tooth (e.g. wants four or five sugars)? The code would soon become tedious and inelegant.

  15. Repetition • Think about this problem in the real world. • In the real world, we would not ask someone if they want sugar and keep repeating the question until the coffee was sufficiently sweet. Q. Do you want a sugar? • Yes. Add a sugar. Q. Do you want another sugar? • Yes Add another sugar Q. etc. • More likely, we would ask how many sugars the person wants and then keep adding spoonfuls until the specified number of sugars has been reached. Q. How many sugars do you want? • Three Add first sugar; add second sugar; add third sugar.

  16. Repetition • In algorithms, this kind of repeated action can be shown using a WHILEloop. WHILE (spaces left on module) Enrol students ; ENDWHILE Send Enrolment papers ; • In a WHILE loop, the action is carried out while the condition is true (e.g. until the condition becomes false). So, as long as sugar is required sugar will be added. • Write a WHILE Loop for adding multiple sugars in the coffee making problem.

  17. Repetition 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. WHILE (sugar required) 9.1 Add spoonful of sugar ; ENDWHILE 10. IF (white coffee required) 10.1 Add milk/cream ; END IF 11.Pour coffee into cup ; 12.Stir coffee ; 13.Turn off machine ; • This is an improvement, but how do we get the program to stop adding sugar if all we know is that sugar is required? • The answer here, as we have seen, is to ask the person we are making coffee for how many sugars they want before we start adding sugars to his coffee. Then to add sugars until the requested number is reached. • How might this be realised in our solution?

  18. Repetition • To add only the required number of sugars we need to keep a count of how many sugars have been added and to compare that number to the number of sugars required each time we pass through the loop. Find out how many sugars required ; WHILE (sugars added not equal to sugars required) Add spoonful of sugar ; Add 1 to number of sugars added ; ENDWHILE

  19. Repetition 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. Find out how many sugars required ; 11.WHILE (sugars added not equalto sugars required) 11.1 Add spoonful of sugar ; 11.2 Add 1 to number of sugars added ; ENDWHILE 12. IF (white coffee required) 12.1 Add milk/cream ; END IF 13.Pour coffee into cup ; 14.Stir coffee ; 15.Turn off machine ; • When the number of sugars added becomes equal to the number required (e.g. the condition becomes false) , then the WHILE loop is exited and the algorithm moves to #12.

  20. Assessing the result • We now need to go through our completed algorithm making sure we have not omitted anything, duplicated anything, or made a logical error. • Can you spot any errors, duplications or omissions?

  21. Assessing the result • What about adding milk/cream? • We know if somebody wants sugar, because we ask them. • But we don’t ask anything with regards milk/cream. Where does this information come from? It must come from somewhere. E.g. We need to find out if our guest wants milk as well as sugar. Adapt your algorithm so that we know whether our guest wants milk as well as sugar.

  22. Assessing the result • It is always a good idea to test the logic of any algorithms and constructs we have designed. • In the case of our WHILE/ENDWHILE loop, we can step through the loop using test values. • Cycle through the loop with a value of zero for sugars required. What happens?

  23. Describing what we have learned and documenting the solution • What did we learn? • A straightforward task (like making coffee) is not as simple as it may seem when we have to instruct a machine to do it for us. • We have to be aware of the assumptions we make. They will determine whether the final solution will be the right one for the original problem. • What difficulties did we encounter? • Dealing with choice and repetition. • To what other circumstances could the solution be generalised? • Any circumstance that involves choice and/or repetition (e.g. feeding a parking meter, entering a password in an ATM, loading a van with parcels).

  24. Documenting the Solution • It is very easy to forget how you arrived at a particular solution. • Documenting your work will serve as a reminder to you and others of the logic of an algorithm, what it does, how it works, etc. • This can be done by providing a short, punchy overview of the solution and explanatory comments interspersed throughout the pseudo code.

  25. Documenting the Solution // Al Gritham Oct 2010 This code shows how to make a cup of coffee for somebody using an electronic percolator. To complete the task, milk and sugar requirements are needed. // Set up coffee machine 1. Put water in coffee machine; 2. Open the coffee holder; 3. Put filter paper in machine; 4. Measure coffee for one cup; 5. Put coffee into filter paper; 6. Shut the coffee holder; 7. Turn on machine; 8. Wait for coffee to filter through; // Deal with milk and sugar requirements 9. Find out how many sugars required; 10. Find out if white coffee required; 11.WHILE (sugars added is not equal to sugars required) 11.1 Add spoonful of sugar; 11.2 Add 1 to number of sugars added; ENDWHILE 12. IF (white coffee required) 12.1 Add milk/cream; END IF // Pour and serve coffee 13.Pour coffee into cup; 14.Stir coffee; // Shut the machine down 15.Turn off machine;

  26. Sessionreading • Vickers • Chapter 4

More Related