Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon Page 1 09 – Problem Solving. Mark Dixon Page 2 Admin: Test 1 Marked One-to-one debriefs, you can –Look at your script –Look at mark scheme.

Similar presentations


Presentation on theme: "Mark Dixon Page 1 09 – Problem Solving. Mark Dixon Page 2 Admin: Test 1 Marked One-to-one debriefs, you can –Look at your script –Look at mark scheme."— Presentation transcript:

1 Mark Dixon Page 1 09 – Problem Solving

2 Mark Dixon Page 2 Admin: Test 1 Marked 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

3 Mark Dixon Page 3 Coursework 1: Plagiarism Cannot share code Cannot share writing Copying code = 0% = fail  Supplying code (giving to others) = 0% Detection Very Likely

4 Mark Dixon Page 4 Coursework 1: Plagiarism Self-plagiarism – you may only submit a piece of work for one assessment Getting others to do work for you: –You can be asked at any time to explain your work –If you cannot explain your work, you may be found guilty of plagiarism

5 Mark Dixon Page 5 Coursework 1: Plagiarism Automated Software:

6 Mark Dixon Page 6 How to: Open web-site to open your work: –start VS2012 –click File menu –click Open Web Site option –navigate to your web site –click your web site (folder) –click Open button  DO NOT USE: File, Save As (this will stop your pages working)

7 Mark Dixon Page 7 Dry run the following code: Dim a Dim b Dim c a = 12 b = a + 5 c = b + 1.25 Questions: Dry Running abc - -- --- 12-- 17- 121718.25

8 Mark Dixon Page 8 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

9 Mark Dixon Page 9 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 –procedural / algorithmic: fixed sequence –do exactly what they are told –cannot deal with errors –no imagination or creativity

10 Mark Dixon Page 10 Dependencies: Numeric Variables consider the following code: 1 Dim h 2 Dim q 3 h = 5 4 q = h + 2 line 3 is dependent on line 1 (it involves h, it needs line 1) line 4 is dependent on line 3 and line 2

11 Mark Dixon Page 11 Dependencies: Data Flow (Pipes) think of connecting pipes (like plumbing in a house): Dim h Dim q h = 5 q = h + 2

12 Mark Dixon Page 12 Dependencies: String Variables consider the following code: 1 Dim surname 2 Dim forename 3 Dim initials 4 surname = "Jones" 5 forename = "Alice" 6 initials = Left(surname, 1) & Left(forename, 1) line 6 is dependent on lines 4 and 5 (it uses the values in the surname and forename variables) line 5 is dependent on line 2 line 4 is dependent on line 1

13 Mark Dixon Page 13 Question: Variable Dependencies What dependencies exist in the following code? Dim q1 Dim q2 Dim u Dim o Dim g q1 = "It is not enough to have a good mind." q2 = "The main thing is to use it well." u = Len(q1) o = Len(q2) g = o + u

14 Mark Dixon Page 14 Inefficient Code duplication in both branches of if If weight > 2.2 Then x = 5x = 5 Else x = 5 End If unused variable declarations Dim xDim x Dim yx = 5 x = 5 redundant (nil effect) lines of code x = 23x = 5 x = 5

15 Mark Dixon Page 15 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

16 Mark Dixon Page 16 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

17 Mark Dixon Page 17 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

18 Mark Dixon Page 18 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

19 Mark Dixon Page 19 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

20 Mark Dixon Page 20 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… (future lecture) –repeated operations do this until… (future lecture) –abstraction – more general description

21 Mark Dixon Page 21 Example: Horizontal mid point Start state: an image on the screen Goal state: to calculate its horizontal mid point Set of operations: –posLeft: gives the position of its left edge –posTop: 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 posLeft value 2. add half the Width

22 Mark Dixon Page 22 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: –Len(s): gives the number of characters in s –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 Solution: 1. Left(text, 1)

23 Mark Dixon Page 23 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

24 Mark Dixon Page 24 Concrete vs. Abstract code we have: –posLeft –posTop right = left + width middle = left + (width / 2) concrete code (works for window 800px wide): picBall.style.posLeft = 400 vs. abstract code (works for all window widths): picBall.style.posLeft = document.body.clientWidth / 2

25 Mark Dixon Page 25 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)

26 Mark Dixon Page 26 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

27 Mark Dixon Page 27 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

28 Mark Dixon Page 28 Example: Swap v1 Swap Sub btnSwap_onClick() txtA.value = txtB.value txtB.value = txtA.value End Sub put txtB into txtA put txtA into txtB does not work!

29 Mark Dixon Page 29 Example: Swap v1 (why?)

30 Mark Dixon Page 30 Example: Swap v2 Swap Sub btnSwap_onClick() txtTemp.value = txtA.value txtA.value = txtB.value txtB.value = txtTemp.value End Sub put txtA into temp put txtB into txtA put temp into txtB works!

31 Mark Dixon Page 31 Example: Swap v2 (Why?)

32 Mark Dixon Page 32 Generating Assignment Code put "Hello" into txtA txtA.value = "Hello" get txtA and join it with txtB, and put the result in parRes parRes.innerText = txtA.value + txtB.value put into txtNum2, the result of multiplying txtNum1 by 2 txtNum2.value = txtNum1.value * 2

33 Mark Dixon Page 33 Questions: Assignment What is the code for: –put 0 into the posLeft property of picHouse –increase the posTop property of picHouse by 5 –decrease the posTop property of picCar by 9 picHouse.style.posLeft = 0 picHouse.style.posTop = picHouse.style.posTop + 5 picCar.style.posTop = picCar.style.posTop - 9

34 Mark Dixon Page 34 Errors txtTemp.value = "" + 5 type mismatch (cannot add "" to 5) txtTemp.value = "7" + 5 OK – VB converts "7" into 7 automatically txtTemp.value = 7 + 5 OK – 7 plus 5 is 12 value.txtTemp = 7 + 5 object required 'value' (object comes first)

35 Mark Dixon Page 35 Nested functions nested functions (one inside another): Right(Left("Hello there", 5), 2) do what is in the brackets first Right(Left("Hello there", 5), 2) = Right( "Hello", 2) = "lo"

36 Mark Dixon Page 36 Example: Text Shift Text Shift Sub window_onLoad() parH.innerText = "Hello There" & Space(100) window.setInterval "TextShift()", 50 End Sub Sub TextShift() parH.innerText = Mid(parH.innerText,2) & Left(parH.innerText,1) End Sub

37 Mark Dixon Page 37 Tutorial Exercises: Swap LEARNING OBJECTIVE: use an algorithm to solve a problem Task 1: get the swap examples (v1 and v2) working Task 2: change v2 of the swap page so that the temporary text box is hidden

38 Mark Dixon Page 38 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.


Download ppt "Mark Dixon Page 1 09 – Problem Solving. Mark Dixon Page 2 Admin: Test 1 Marked One-to-one debriefs, you can –Look at your script –Look at mark scheme."

Similar presentations


Ads by Google