Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 6: Problem Solving Exercises Stewart Blakeway FML.

Similar presentations


Presentation on theme: "Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 6: Problem Solving Exercises Stewart Blakeway FML."— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 6: Problem Solving Exercises Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113 http://hopelive.hope.ac.uk/computing/

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Seen what an algorithm is – a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in – Structured English – Flow charts Used variables to remember Applied the top down, stepwise refinement approach to creating algorithms 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today Work towards writing algorithms to be used in computer programs Solve problems following the “rules of the game” 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Tighten up a Little So far, we have laid down fairly strict rules governing the programming constructs and how they should be set out We have given guidelines for choice of variable names All of the algorithms developed so far have been fairly lax in the choice of phraseology 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Tighten up a Little We are heading towards learning how to program a computer to execute algorithms. Algorithms will be expressed as computer programs written in specialised programming languages Computer programming languages have strict rules of grammar. This set of rules constitutes the syntax of that particular language Computer languages accumulate libraries of special commands to enable complex algorithms to be implemented quickly in many contexts 5

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Programming libraries PieEater – a learning library – turnleft(); – turnright(); – randompies(20); 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Tighten up a Little This week - a bridge between algorithmic design and actual computer programs which run correctly – Specify the rules – Specify the syntax – Specify the problem – Test the solution 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE This Will Certainly Help AB A AND B AB A OR B False TrueFalse True False TrueFalseTrue 8

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE This Will Certainly Help AB A AND B AB A OR B False TrueFalse True False TrueFalseTrue 9 If today is Tuesday AND it is summer begin Go and play bowls end Needs to both be Tuesday as well as summer to play bowls One of them is not enough

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE This Will Certainly Help AB A AND B AB A OR B False TrueFalse True False TrueFalseTrue 10 If today is Tuesday OR it is sunny begin Go and play bowls end Needs only one to be true to play bowls One of them is enough

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks a Specification The Environment The Rules The Syntax for the allowed operations 11

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Operation LIFO 12 Last in First Out

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Empty stack 13

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Items added to stack 14 A B C D Always added to top of stack

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Items removed from stack 15 A B C D Always removed from top of stack

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks A stack is a key data structure used in computing Used to perform complex calculations to keep track of what is being added to what etc – e.g. A = X*(Y+Z)/(X*X – Y*Y) 16

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks A stack is a key data structure used in computing Used to pass values between program parts 17

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Environment A box containing a pile of books The number of books in the box is unknown (The box is labelled Stack1) A store of similar empty boxes labelled Stack2, Stack3…. The boxes are called Stacks. A tray which can hold 1 book only. (This is labelled Temp) 18

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Rules A book may only be taken from the top of a Stack An attempt to take a book off an empty Stack is illegal A book can only be moved from a Stack to a Tray or vice versa A Stack will never become full 19

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Syntax for the Allowed Operations Get Stackn – Gets an empty box labelled Stackn from the store of boxes Pop(Stackn, Temp) – Moves a book from the top of Stackn to the Tray Push(Temp, Stackn) – Moves a book from the Tray to the top of Stackn 20

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Starting position 21 Temp

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Get Stack4 22 Temp Stack4

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Temp := A 23 Temp Stack4 A

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 24 Temp Stack4 A A

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Temp := B 25 Temp Stack4 B A

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 26 Temp Stack4 B A B

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Temp := C 27 Temp Stack4 B A C

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 28 Temp Stack4 C A B C

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Specify the Problem Generate a Stack of books, labelled Stack2, which are in the reverse order to those in Stack1 29

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Before … 30 A B C Stack1

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE After … 31 Stack1 C B A Stack2

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Top Level Solution 32 Reverse(Stack1, Stack2) This is a shorthand way of specifying the problem Remember Stack1 is given, but Stack2 is still stored away.

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A Better Solution Would Be 33 Get Stack2 Reverse(Stack1, Stack2)

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Level Solution 34 Repetition Move a book from Stack1 to Stack2 When does this terminate ? When Stack1 is empty (remember Stacks never become full) What is the opposite of this When Stack1 is not empty (Can be written as Stack1 not empty)

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Algorithm So Far 35 Get Stack2 while Stack1 not empty begin move a book from Stack1 to Stack2 end

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Move a book from Stack1 to Stack2 36 We must follow the rules of the game. Stack1TrayStack2 Pop Push

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Solution 37 Get Stack2 while Stack1 not empty begin Pop(Stack1, Temp) Push(Temp, Stack2) end A B C Stack 1 Stack 2 TRUE C B A CBA FALSE

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Problem Given – A store of empty boxes, labelled Stack3, Stack4… – Stack1 and Stack2, each containing an unknown number of books – A tray labelled Temp Rules – As before Problem Specification – Generate a Stack of books, labelled Stack3, comprising the books from Stack1 merged with the books from Stack2 38

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 39 Do You Understand the Problem ?

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Understanding the Problem 40 Book 1Book A Book 2Book B Book 3Book C...... Book ??? Stack1Stack2 Before the Merge

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Understanding the Problem Book1 BookA Book2 BookB... Book ??? 41 After the Merge Stack3

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Top Level Solution 42 Get Stack3 Merge(Stack1, Stack2, Stack3) Obvious from previous example Possible way of expressing the problem

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Top Level Solution 43 Get Stack3:No more refinement needed Merge(Stack1, Stack2, Stack3) It seems to be a repetition of: Stack1 Stack2 Tray Stack3 First move Second move Third move

44 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A Repetition of What ? 44 A repetition of: Pop(Stack1, Temp) Push(Temp, Stack3) Pop(Stack2, Temp) Push(Temp, Stack3)

45 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE We Now Have 45 Get Stack3 while not a clue yet but am working on it begin Pop(Stack1, Temp) Push(Temp, Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end

46 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What is the Problem ? 46 The major problem is that the initial states of the 2 Stacks are unknown. Both may be empty. One may be empty. One may contain 99 books and the other 46 books etc. We want the algorithm to work for any two Stacks

47 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let us Focus on the while Test 47 When do we stop merging the 2 stacks ? When any further action would be illegal. What action is defined as illegal ? An attempt to take a book off an empty stack. Solution: Stop merging as soon as one stack becomes empty Then what? Fill up stack3 with the remaining books in the other stack

48 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Seems Easy But What is the Problem ? 48 Unknown starting states, as before. Let us look at some possible solutions and test them.

49 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution A 49 Get Stack3 while EITHER Stack1 OR Stack2 is not empty begin Pop(Stack1, Temp) Push(Temp, Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end ALWAYS TRUE EXCEPT WHEN BOTH CONDITIONS EVALUATE TO FALSE

50 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution B 50 Get Stack3 while BOTH Stack1 AND Stack2 are not empty begin Pop(Stack1, Temp) Push(Temp, Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end ALWAYS FALSE EXCEPT WHEN BOTH CONDITIONS EVALUATE TO TRUE

51 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution C 51 Get Stack3 while BOTH Stack1 AND Stack2 are not empty begin Pop(Stack1, Temp) Push(Temp, Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end

52 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution C Continued 52 while Stack1 not empty begin Pop(Stack1, Temp) Push(Temp, Stack3) end while Stack2 not empty begin Pop(Stack2, Temp) Push(Temp, Stack3) end This will empty Stack1 if it is not empty. If it is already empty, this section of code is leapfrogged over. This will empty Stack2 if it is not empty. If it is already empty, this section of code is leapfrogged over.

53 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 53 We have certainly merged the two Stacks, but the resulting Stack, Stack3, does not quite fit the problem specification. Why Not ???

54 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Very Nearly There 54 We have merged the two stacks, but at the moment Stack3 is upside down. It needs reversing. The solution is at hand. We have already created an procedure Reverse. The syntax we chose was Reverse(Stacka, Stackb) We now have a procedure Merge which merges two stacks, but produces the correct result in the reverse order. The syntax for this was Merge(Stackp, Stackq, Stackr)

55 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE At Last The Solution 55 Get Stack3 Get Stack4 Merge(Stack1, Stack2, Stack4) Reverse(Stack4, Stack3) Procedure Calls Parameters

56 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Question 56 Using procedure calls and appropriate parameters, write down the solution to the following problem. Given: 3 stacks, Stackx, Stacky and Stackz containing an unknown number of books. Problem: Merge Stackx with Stacky so that the contents of the resulting Stack, Stackw, are in the correct order. Merge Stackw with Stackz so that the contents of the resulting stack, Stackn, are in the correct order.

57 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution 57 Get Stackw Get Stackm Get Stackn Merge(Stackx, Stacky, Stackn) Reverse(Stackn, Stackw) Merge(Stackw, Stackz, Stackm) Reverse( Stackm, Stackn)

58 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 58 This is a valid solution which involves generating (getting) 3 empty Stacks (Stackw, Stackm & Stackn). There is a better solution which only generates 2 empty Stacks. Have a go !

59 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 59 Get Stackw Get Stackn Merge(Stackx, Stacky, Stackn)Stackx, Stacky now empty Reverse(Stackn, Stackw)Stackn now empty Merge(Stackw, Stackz, Stackx) Reverse( Stackx, Stackn)

60 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE More interesting stuff … 60

61 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Supplementary Exercise 1 page 61 Operations used: – Take(Potato, Skip) – Place(Potato, G) – Place(Potato, P) These are actions only - no values generated (returned) 61

62 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Boolean Functions return True or False Boolean function used: – Gauge(Potato) Returns a value of True or False 62

63 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE How to use operations and functions while there are potatoes in the skip begin Take(Potato, Skip) if Gauge(Potato) = True then begin Place(Potato, G) end else begin Place(Potato, P) end 63

64 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternatively while there are potatoes in the skip begin Take(Potato, Skip) if Gauge(Potato) then begin Place(Potato, G) end else begin Place(Potato, P) end 64

65 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Counting Everything Remember to Initialise Variables to zero Example: Count all Potatoes Sampled: Count := 0 while there are potatoes in the skip begin Count := Count +1 other stuff end 65

66 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Selecting then Counting Example: Generate separate totals for useful and useless potatoes. Useful := 0 Useless := 0 if Gauge(Potato) = True then begin Useful := Useful + 1 Place(Potato, G) end else begin Useless := Useless + 1 Place(Potato, P) end 66

67 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Altogether now Useful := 0 Useless := 0 while there are potatoes in the skip begin Take(Potato, Skip) if Gauge(Potato) = True then begin Useful := Useful + 1 Place(Potato, G) end else begin Useless := Useless + 1 Place(Potato, P) end Display Useful, Useless 67

68 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Supplementary Exercise 2: Page 62 Functions returning values: Direction(Customer) Returns IN or OUT Sway(Customer) Returns High, Medium or Low 68

69 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Another Boolean Function Light_On Returns True or False 69

70 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE H := 0 M := 0 L := 0 while Light_on = True begin if Direction(customer) = OUT then begin if Sway(customer) = High then begin H := H + 1 end if Sway(customer) = Medium then begin M := M + 1 end if Sway(customer) = Low then begin L := L + 1 end Display H, M, L 70


Download ppt "Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 6: Problem Solving Exercises Stewart Blakeway FML."

Similar presentations


Ads by Google