08 Problem Solving and Algorithms Mark Dixon Page
08 – Problem Solving and Algorithms Mark Dixon Page 1
Admin: Test 1 • Marked – just doing final checks • One-to-one debriefs, you can – Look at your script – Look at mark scheme – Check accuracy of marking – ask questions • Cannot question marking scheme • Can question consistency of marking Mark Dixon Page 2
Questions: Dry Running • Dry run the following code: var a; var b; var c; a = 12; b = a + 5; c = b + 1. 25; Mark Dixon a - b c - - - 12 17 18. 25 Page 3
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 Mark Dixon Page 4
Humans vs. Computers • Humans and Computers work very differently • Humans – – – declarative (goals): flexible sequence intelligent: adaptive, questioning, rational instinctive (without conscious thinking) easily deal with incomplete and incorrect data error prone (especially mundane repetitive tasks) • Computers – – Mark Dixon procedural / algorithmic: fixed sequence do exactly what they are told cannot deal with errors no imagination or creativity Page 5
Inefficient Code • duplication in both branches of if if (weight > 2. 2){ x = 5; }else{ x = 5; } • unused variable declarations var x; var y; x=5 • redundant (nil effect) lines of code x = 23; x = 5; Mark Dixon x = 5; var x; x = 5; Page 6
Types of problem • 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 Mark Dixon Page 7
What is a problem? • All problems 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 Mark Dixon Page 8
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 – small number of operations to solve Mark Dixon Page 9
Example: Movement • Goal state • Start state • Operations – Move Up – Move Down – Move Left – Move Right • Solution: Mark Dixon 1. 2. 3. 4. 5. 6. Move Up or Move Up Move Right 1. 2. 3. 4. 5. 6. Move Up Move Right Page 10
Important steps • essential to understand the problem (start state, goal state, and operations) • also often essential 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 Mark Dixon Page 11
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… (lecture 5) – repeated operations do this until… (future lecture) – abstraction – more general description Mark Dixon Page 12
Example: Horizontal mid point • Start state: an image on the screen • Goal state: to calculate its horizontal mid point • Set of operations: – – pos. Left: gives the position of its left edge pos. Top: 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 pos. Left value 2. add half the Width Mark Dixon Page 13
Example: First Character • Start state: a piece of text (e. g. "Hello") • Goal state: extract the first character • Set of operations: – length: gives the number of characters – char. At(n): gives the nth character – slice(s, e): gives characters between position s and e – substr(p, n): gives n characters from position p • Solution: 1. substr(0, 1) Mark Dixon Page 14
Concrete vs. Abstract problems • People can solve concrete problems easily – what is the first letter of hello – what is the first letter of your 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 Mark Dixon Page 15
Concrete vs. Abstract code • we have: – pos. Left – pos. Top • right = left + width • middle = left + (width / 2) • concrete code (works for window 800 px wide): pic. Ball. style. pos. Left = 400; • vs. abstract code (works for all window widths): pic. Ball. style. pos. Left = document. body. client. Width / 2; Mark Dixon Page 16
Algorithms • algorithm: step-by-step sequence of instructions, to solve a problem • it describes how input data is to be processed in order to produce a desired output • similar to recipe – ingredients (similar to data) – method (is a type of algorithm) Mark Dixon Page 17
Algorithms • Making a cup of tea: 1. Fill the kettle with water 2. Plug the kettle in 3. Switch the kettle on 4. Wait for the kettle to boil 5. Put water in cup 6. Put a tea bag into the cup 7. Add sugar to the cup 8. Add milk to the cup 9. Stir 10. Take the tea bag out Mark Dixon Page 18
What vs. How • what vs. how: – What: increase value of a text box by 1 – How: • read the current value • add 1 • put the result back in the text box • For example: swap, search, sort Mark Dixon Page 19
Example: Swap v 1 • does not work! • put txt. B into txt. A • put txt. A into txt. B Mark Dixon <html> <head><title>Swap</title></head> <body> <input id="txt. A" type="text" value="Bob" /> <input id="txt. B" type="text" value="Sally" /> <input id="btn. Swap" type="button" value="S onclick="btn. Swap_on. Clic </body> </html> <script language="javascript"> function btn. Swap_on. Click(){ txt. A. value = txt. B. value; txt. B. value = txt. A. value; } </script> Page 20
Example: Swap v 1 (why? ) Mark Dixon Page 21
Example: Swap v 2 • works! • put txt. A into temp • put txt. B into txt. A • put temp into txt. B Mark Dixon <html> <head><title>Swap</title></head> <body> <input id="txt. A" type="text" value="Bob" /> <input id="txt. B" type="text" value="Sally" /> <input id="btn. Swap" type="button" value="Swa onclick="btn. Swap_on. Click()" /> <input id="txt. Temp" type="text" disabled="disa </body> </html> <script language="javascript"> function btn. Swap_on. Click(){ txt. Temp. value = txt. A. value; txt. A. value = txt. B. value; txt. B. value = txt. Temp. value; } </script> Page 22
Example: Swap v 2 (Why? ) Mark Dixon Page 23
Generating Assignment Code • put "Hello" into txt. A. value = "Hello"; • get txt. A and join it with txt. B, and put the result in par. Res. inner. Text = txt. A. value + txt. B. value; • put into txt. Num 2, the result of multiplying txt. Num 1 by 2 txt. Num 2. value = txt. Num 1. value * 2; Mark Dixon Page 24
Questions: Assignment • What is the code for: – put 0 into the pos. Left property of pic. House. style. pos. Left = 0; – increase the pos. Top property of pic. House by 5 pic. House. style. pos. Top = pic. House. style. pos. Top + 5; – decrease the pos. Top property of pic. Car by 9 pic. Car. style. pos. Top = pic. Car. style. pos. Top – 9; Mark Dixon Page 25
Errors • txt. Temp. value = "" + 5; type mismatch (cannot add "" to 5) • txt. Temp. value = "7" + 5; OK – VB converts "7" into 7 automatically • txt. Temp. value = 7 + 5; OK – 7 plus 5 is 12 • value. txt. Temp = 7 + 5; object required 'value' (object comes first) Mark Dixon Page 26
Example: Text Shift <html> <head><title>Text Shift</title></head> <body onload="window_on. Load()"> <p id="par. H"></p> </body> </html> <script language="javascript"> function window_on. Load(){ par. H. inner. Text = "Hello There "; window. set. Interval("Text. Shift()", 200); } function Text. Shift(){ par. H. inner. Text = String(par. H. inner. Text). slice(1) + String(par. H. inner. Text). substr(0, 1); } </script> Mark Dixon Page 27
Problem Solving: 9 dots • Join all 9 dots – with 4 straight continuous lines Mark Dixon Page 28
Problem Solving Process (Name Split) • Problem: a variable exists called n. This contains a person's full name (forename, then surname ). It needs to be split into two separate variables. var n; n = "Ruth Jones"; • Solution Process: – What do I do to solve this manually (on paper)? – How do I know where the forename ends and the surname begins? – The space is the key: • Find the space • everything before the space is the forename • everything after the space is the surname Mark Dixon Page 29
Example: Drinks v 1 Total of array Clears array Displays array Mark Dixon Searches array Page 30
Example: Drinks v 1 Dim Units(6) Dim cur. Unit Sub window_on. Load() cur. Unit = 0 End Sub btn. Add_on. Click() Units(cur. Unit) = txt. Unit. value cur. Unit = cur. Unit + 1 End Sub btn. Clear_on. Click() Units(0) = 0 Units(1) = 0 Units(2) = 0 Units(3) = 0 Units(4) = 0 Units(5) = 0 Units(6) = 0 cur. Unit = 0 End Sub btn. Show_on. Click() par. Res. inner. Text = "" par. Res. inner. Text = par. Res. inner. Text & Units(0) & " " par. Res. inner. Text = par. Res. inner. Text & Units(1) & " " par. Res. inner. Text = par. Res. inner. Text & Units(2) & " " par. Res. inner. Text = par. Res. inner. Text & Units(3) & " " par. Res. inner. Text = par. Res. inner. Text & Units(4) & " " par. Res. inner. Text = par. Res. inner. Text & Units(5) & " " par. Res. inner. Text = par. Res. inner. Text & Units(6) End Sub Mark Dixon …. Page 31
Example: Drinks v 1 …. Sub btn. Total_on. Click() Dim total = 0 total = total + Units(0) total = total + Units(1) total = total + Units(2) total = total + Units(3) total = total + Units(4) total = total + Units(5) total = total + Units(6) par. Res. inner. Text = total End Sub btn. Find_on. Click() If txt. Unit. value = Units(0) Then par. Res. inner. Text = "Found in slot 0" Else. If txt. Unit. value = Units(1) Then par. Res. inner. Text = "Found in slot 1" Else. If txt. Unit. value = Units(2) Then par. Res. inner. Text = "Found in slot 2" Else. If txt. Unit. value = Units(3) Then par. Res. inner. Text = "Found in slot 3" Else. If txt. Unit. value = Units(4) Then par. Res. inner. Text = "Found in slot 4" Else. If txt. Unit. value = Units(5) Then par. Res. inner. Text = "Found in slot 5" Else. If txt. Unit. value = Units(6) Then par. Res. inner. Text = "Found in slot 6" Else par. Res. inner. Text = "Not Found" End If End Sub Mark Dixon Page 32
Array Algorithms • Common tasks to many programs: – Reset all elements – Display all elements – Total all elements – Search all elements – Find maximum value – Find minimum value – Average – Sort Mark Dixon Page 33
Example: Drinks v 2 (Total) Mark Dixon Page 34
Tutorial Exercises: Swap • LEARNING OBJECTIVE: use an algorithm to solve a problem • Task 1: get the swap examples (v 1 and v 2) working • Task 2: change v 2 of the swap page so that the temporary text box is hidden Mark Dixon Page 35
Tutorial Exercises: Text Shift • LEARNING OBJECTIVE: develop and implement an algorithm to solve a problem use string manipulation functions, and sound • Task 1: get the Text Shift example (from the lecture) working • Task 2: modify your program so that the text goes the other way. • Task 3: modify your program so that a noise is made when the user moves the mouse over the text. Mark Dixon Page 36
Tutorial Exercise: Drinks • Task 1: Get the Drinks v 2 example (from the lecture) working. You have the code for Add, Clear, & Show but not for Total and Find • Task 2: Modify your page so that it displays a meaningful message when all elements of the array are used up (not the error dialogue below). Mark Dixon Page 37
- Slides: 37