Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

26
Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition

Transcript of Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

Page 1: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

Problem Solving for Programming

Session 3Dealing with

Sequence, Choice and Repetition

Page 2: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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

Swing out holder for the filter

Coffee jugOn/off switch

Using an electronic filter machine (also called a percolator) make a pot of coffee and pour a cup.

Page 3: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 theproblem (3) Carry out the plan (4) Assess the result (5) Describe what has been learned from the process (6) Document the solution.

1. Put water and coffee in the machine ;

2. Turn on the machine ;

3. Pour coffee ;

1. Turn on the machine ;

2. Put water and coffee in the machine ;

3. Pour coffee ;

Page 4: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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?

Page 5: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 problem1) 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.

Page 6: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 7: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 8: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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

Page 9: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 ;

Page 10: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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?

Page 11: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 12: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Is the condition true or false?

Carry out the action

Carry out next task

True/Yes

False/No

Page 13: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 ; ENDIF10.IF (white coffee required)

10.1 Add milk ; ENDIF11.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?

Page 14: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 15: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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?A. Yes. Add a sugar.Q. Do you want another sugar?A. Yes Add another sugarQ. 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?A. Three Add first sugar; add second sugar; add third sugar.

Page 16: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

Repetition

• In algorithms, this kind of repeated action can be shown using a WHILE loop.

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.

Is the condition true or false?

Carry out the action

Carry out next task

True

False

Page 17: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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

Page 18: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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

Page 19: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 equal to sugars

required)11.1 Add spoonful of sugar ;11.2 Add 1 to number of sugars added ;

ENDWHILE12. IF (white coffee required)

12.1 Add milk/cream ; END IF13.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.

Page 20: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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?

Page 21: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 22: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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?

Iteration Sugars required

Sugars added

Condition true/false?

Action taken

1 2 0 True Add spoonful of sugar

2 2 1 True Add spoonful of sugar

3 2 2 False Exit WHILE loop

Page 23: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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

Page 24: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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.

Page 25: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

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 requirements9. 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;

ENDWHILE12. IF (white coffee required)

12.1 Add milk/cream; END IF// Pour and serve coffee13.Pour coffee into cup;14.Stir coffee;// Shut the machine down15.Turn off machine;

Page 26: Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.

Session reading

• Vickers – Chapter 4