Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Similar presentations


Presentation on theme: "Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies."— Presentation transcript:

1 Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies

2 How do we solve problems? There are many different kinds of problems we need to solve as programmers. Some are complex and very difficult to solve. In order to solve complex and difficult problems, we need: – an appropriate way of describing the problem (a language of description) –an appropriate way of solving the problem (a problem solving strategy).

3 Languages of description – natural language The most accessible tool at our disposal as problem solvers is natural language. Natural language allows us to describe problems in a way that can easily be understood by everybody. However, natural language is often imprecise and ambiguous (e.g. He eats shoots and leaves as opposed to He eats, shoots and leaves), which is of little use when we are writing computer programs, which need to be clear, precise and unambiguous. Because it can be imprecise and ambiguous, natural language should be only one tool amongst many that we use when problem solving. Natural language needs to be augmented with more precise ways of representing problems.

4 Languages of description – visual representation An alternative to natural language, and an important tool in a problem solver’s armoury, is visual or graphical representation. Visual representation enables us to make complex problems easier to understand and solve. Take the following example:

5 Languages of description – visual representation One morning at eight o’clock you set off in your car to visit a friend who lives some distance away. You encounter the odd traffic delay and your driving speed varies as the speed limits on the different stretches of road change. It is a long drive, so you stop twice to refresh yourself and to eat and drink something. You arrive at 6.00 p.m. and decide to stay overnight. The next morning you set off again at 8.00 a.m. You take the same route back, but encounter heavy traffic in different places. Because one traffic jam was so large you only stop once for refreshments. Still, you arrive back home at six o’clock. Question: Is there a point on the road that you pass at exactly the same time of day on the outward and return journeys? Trying to make sense of this problem on the basis a natural language description like this is difficult. There is a lot of detail to store in your head.

6 Languages of description – visual representation A visual representation of the problem, on the other hand, allows us to see the problem and the solution more clearly. (Assuming the same average speed),we can see that the point is reached at 2pm. Six hours into the journey.

7 Languages of description – visual representation The same problem can often be represented in many different ways. For example, you could represent the journey problem numerically like this. The best representation is the way that suits you (And, of course, the way that produces a correct solution!).

8 Languages of description – visual representation Problems involving two or three dimensional space are very difficult to solve without some form of visual representation. Take the following: There is a large square room whose walls are twenty-four feet long. The ceiling is eight feet high. On the floor in a corner is a bowl of sugar. In the opposite corner by the ceiling is an ant. Question: What is the shortest path the ant can take to get to the sugar? The problem appears to be maths/geometry based, but even a somebody with a good head for maths might struggle with this one without some form of picture to assist him/her.

9 Languages of description – visual representation The quickest route would appear to be straight down the wall and diagonally across the room to the sugar (A). However, we need to consider other possibilities. What about (B)? Is this shorter? How can we test if it is or not?

10 Languages of description – taking a different view The first one (A) is easy to test using Pythagoras. Now, what about the second one (B)? It appears we do not have sufficient information at our disposal to calculate the distance of the route. But what if we take a different view of the problem? What if we represent the problem two-dimensionally instead of three-dimensionally?

11 Languages of description – taking a different view Three dimensional Two dimensional Can we solve the problem now? How?

12 Languages of description – taking a different view It is now possible to draw a straight line from ant to sugar to create a triangle whose width and length are known, meaning, with a little help from Pythagoras, we can easily work out the distance we need. As a result, we can see that B is, in fact, the shortest path. This solution was only possible, because we took an alternative view of the problem.

13 Other problem solving strategies – solving a simpler analogous problem Another approach to solving complex problems may be to solve a simpler analogous problem first. Recall the problem about meeting on the road. In this problem, one person makes two journeys on two different days. What about if two people make the same journey (in reverse) on the same day. Of course they will meet, and of course they will meet at the same time. By rephrasing a problem in simpler terms, we can often see the solution more clearly.

14 Other problem solving strategies – finding a pattern Consider the following problem: How many angles are formed by ten distinct rays with a common end point?

15 Other problem solving strategies – finding a pattern Let’s start simply (again solving a simpler analogous problem). How many angles are formed by one ray? Two rays? Three? The answer is clear here because there is little detail to consider (0, 1, 3).

16 Other problem solving strategies – finding a pattern If we represent this data in a table, we get the following. Can you see a pattern emerging? What about four or five rays? Can you see a pattern emerging now? No. rays123 No. angles013 No. rays12345 No. angles0136?

17 Other problem solving strategies – working backwards Consider the following problem: Helen has an 11-litre can and a 5-litre can. How can she measure out exactly seven litres of water?

18 Other problem solving strategies – working backwards We could try to measure out the 7-litres using a combination of cans through trial and error. However, if we consider where we are at the end of the problem, we can see that our can has 7-litres of water and 4-empty litres. Can we measure off four litres then? The answer is, yes. If we empty the 11-litre can into the 5-litre can twice, we know we have 1-litre left. If we empty this into a 5-litre can, we know the empty part is four litres. Now if we refill the 11-litre can, and pour off 4-litres into the 5-litre can, we are left with 7-litres in the 11-litre can.

19 Other problem solving strategies – intelligent guessing Guessing solutions is not usually a good strategy, but intelligent guessing can be Consider the following problem: The one mile relay team has four runners: Bob, Ian, Len and Sid. They run in alphabetical order. Each runner runs his quarter-mile 2 sec faster than the next runner. They finish in exactly 3mins 40 sec. Question: How fast did each runner run his part?

20 Other problem solving strategies – intelligent guessing It will help if we start by converting 3 min 40 sec to seconds (e.g. 220 sec). As a starting point for our guessing, we could imagine the runners are evenly matched, and run at the same speed. They would then run their respective parts of the race at 220/4 = 55 sec. We can use this for our initial guess for our slowest runner, Sid. But this is obviously wrong as 208<220. We know Sid has to run slower than 55 sec. So we need to increase his time. Runner:BobIanLenSidTotal Time:49515355208

21 Other problem solving strategies – intelligent guessing If we up our guess by one, we are still wrong. However, with a couple more (intelligent) guesses we eventually arrive at the correct solution. BobIanLenSidTotal 49515355208 50525456212 51535557216 52545658220

22 Other problem solving strategies – logical reasoning Consider the following problem: Place the numbers 1 – 9 in the following grid so that the sum of all the rows, columns and diagonals is the same.

23 Other problem solving strategies – logical reasoning Start by placing all the numbers in the grid sequentially 1 – 9. The sum of all the cells is 45. If each row is to have the same sum, then it must be 15 (45/3). The next step is to determine which number should be entered into the centre cell. Start by trying extreme cases. If we entered 9, then 9 would appear in a row, column or diagonal with 8, 7 and 6, giving a total > 15. If we entered 1, then 1 would appear in a row, column or diagonal with 2, 3 and 4, giving a total < 15. Having accounted for all the possibilities, we can see now that only 5 can occupy the centre cell.

24 Other problem solving strategies – logical reasoning Now we can use intelligent guessing. We could try  The problem here, however, is that with nine in one column, the other two numbers in that column must total 6 (e.g. 2 & 4). But if 2 and 4 are in the column, then the row with 1 in it can never total 15. If we move our 1, 5 and 9 to the central column, however, we do not have the same problem. The rest is easy. 1 5 9 14 52 9 1 5 492 816 357 492

25 Mathematical reasoning Mathematical reasoning underlies several of the problem solving strategies we have already looked at. For example: You do not need to be brilliant at maths to be a competent problem solver. But being good at maths will certainly help you to solve programming problems faster and more effectively.

26 Algorithms After we have solved a problem, we can start to develop algorithms to express our solutions in general terms. If you recall, in session 1 we developed a solution for the following problem: In a room with ten people, everyone shakes hands with everyone else exactly once. In total, how many handshakes are there? The solution to the problem was as follows: grid size = 10x10 = 100 handshakes = (100-10)/2 = 45 The solution was correct, but it would be of little use as a computer program, as it is specific to a particular number of participants (10). We saw that it would be much more useful to develop a general formula that is applicable to all instances of the problem: for example h = ((p 2 ) – p)/2

27 Algorithms Algorithms are like this formula. They are generalisable to other instances of the same problem (e.g. 20, or 100, 10,000, n participants).

28 Algorithms Imagine we have a Smartphone and we want to develop an app for it that will solve the handshakes problem we encountered in session 2. Would it be enough to tell the device: h = (p 2 – p)/2 Almost certainly not. There is simply not enough information available for a computer to know what to do. For example, we have not said what p is, or for that matter h, or where these values come from. Also, what does p 2 mean to a computer? We need to provide more detail and to express the algorithm in a way a computer can understand in order for our program to work effectively. We need to provide a detailed, step-by-step set of instructions: in other words a concise and complete algorithm.

29 Algorithms and pseudo code Algorithm development is the intermediate stage between problem solving and coding. It is where we specify the solution to a problem in a generalisable and structured format somewhere between natural language and programming code. The standard way that programmers have of creating algorithms is to use pseudo code (sometimes called structured English). Pseudo code is a high-level description of a computer program that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Importantly, it is programming language independent, which means you do not have to know a particular programming language to understand how it works.

30 Algorithms and pseudo code 1. Request input ; 2. Check input valid ; 3. IF (input valid) 3.1 persons  input ; ENDIF 4. handshakes = ((persons * persons)- persons)/2 ; 5. Display handshakes ; Task number Line terminator Tasks or steps in the algorithm

31 Sequence As we have seen, an algorithm is a basic representation of the steps involved in a problem. When we are developing algorithms, it is important that the sequence of the steps is represented correctly. Consider the following example of an ATM: 1. Get password ; 2. Get amountNeeded ; 3. Dispense amountNeeded ; 4. IF (enteredPassword is NOT equal to storedPassword) 4.1 Do not dispense amountNeeded ; END IF What is wrong?

32 Summary We need to represent our problems in the most appropriate language of description. We need to select the best problem-solving strategy. The difficulty, however, is learning which language of description and problem solving strategy to chose for a particular problem scenario. This will come with experience and practice.

33 Session reading Vickers –Chapter 3


Download ppt "Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies."

Similar presentations


Ads by Google