Download presentation
Presentation is loading. Please wait.
1
Mark Dixon Page 1 22 – Problem Solving
2
Mark Dixon Page 2 Session Aims & Objectives Aims –to provide a more explicit understanding of problem solving skills and strategies Objectives, by end of this week’s sessions, you should be able to: –recognise the key aspects of a problem start state goal state operations –be able to use typical strategies to solve unfamiliar programming problems
3
Mark Dixon Page 3 Types of problem There are two types of problem: –Known problems: which we have successfully dealt with before, and can remember the solution –Unknown problems: which we have never seen before, and therefore have to discover / invent a solution for ourselves
4
Mark Dixon Page 4 What is a problem? All problems are different However, have key parts: –Start state –Goal state –set of available operations Problem solving is the process of searching for a sequence of operations that will take us from the start state to the goal state
5
Mark Dixon Page 5 Important steps It is essential to understand the problem (start state, goal state, and operations) prior to finding a solution It is also often essential to be able to break a problem down into smaller sub- problems, i.e. identify intermediate sub-goal states failure to solve a problem is often due to these
6
Mark Dixon Page 6 Example: Light Start state: light is off Goal state: light on Set of operations: –Push switch up (turns light on) –Push switch down (turns light off) Solution: 1. Push switch up Simple problems involve a small number of operations to solve
7
Mark Dixon Page 7 More Complex Problems More complicated problems involve –the use of multiple operations to get from the start to the goal state –conditional execution of operations (only do this if…) –repeated operations (do this until…) –abstraction (more general description)
8
Mark Dixon Page 8 Example: First Character Start state: the piece of text (e.g. "Hello") Goal state: extract the first character of a piece of text Set of operations: –Right(s, n): gives n characters from the right of s –Left(s, n): gives n characters from the left of s –Mid(s, p, n): gives n characters from s, starting at position p –Len(s): gives the number of characters in s Solution: 1. Left(text, 1)
9
Mark Dixon Page 9 Example: Horizontal mid point Start state: an image on the screen Goal state: to calculate its horizontal mid point Set of operations: –pixelLeft: gives the position of its left edge –pixelTop: gives the position of its top edge –Width: gives the distance between its left and right edges –Height: gives the distance between it top and bottom edges Solution: 1. get the pixelLeft value 2. add half the Width
10
Mark Dixon Page 10 Concrete vs. Abstract problems People can solve concrete problems easily –what is the first letter of hello –what is the first letter of you surname –an object's left edge is at position 100 the object is 50 wide where is its mid point? It is often difficult for them to describe the general (abstract) process they use
11
Mark Dixon Page 11 Goal state Start state Operations –Move Up –Move Down –Move Left –Move Right Solution: Example: Movement 1.Move Up 2.Move Up 3.Move Up 4.Move Right 5.Move Right 6.Move Right 1.Move Up 2.Move Right 3.Move Up 4.Move Right 5.Move Up 6.Move Right or
12
Mark Dixon Page 12 Example: Movement (abstract) Abstract (general) solution: 1.If current is below goal then Move Up 2.ElseIf current is above goal then Move Down 3.ElseIf current is left of goal then Move Right 4.ElseIf current is right of goal then Move Left 5.Else Stop (current = goal) 6.Go To 1 (loop)
13
Mark Dixon Page 13 Flowcharts often useful for representing algorithms: below goal above goal left of goal right of goal Start Move Up Move Down Move Right Move Left Stop Y Y Y Y N N N N
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.