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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

AE1APS Algorithmic Problem Solving John Drake
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
 Control structures  Algorithm & flowchart  If statements  While statements.
Algorithm –History Muhammad ibn Musa Al-Khwarizmi www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
ITEC113 Algorithms and Programming Techniques
Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway
Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Data Structures 3 Stewart Blakeway FML
Introduction to Computability Theory
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 2: Algorithm Discovery and Design
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
The Program Design Phases
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Chapter 2 - Algorithms and Design
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
By the end of this session you should be able to...
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Visual Basic Programming
Algorithm Design.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
1 CS161 Introduction to Computer Science Topic #9.
Data Structures & Algorithms
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Lecture 2 Introduction to Programming
MSIS 655 Advanced Business Applications Programming
Introduction to Problem Solving and Control Statements
Algorithm and Ambiguity
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 6: Problem Solving Exercises Stewart Blakeway FML

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

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

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

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

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

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

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

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

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

Faculty of Sciences and Social Sciences HOPE Stacks a Specification The Environment The Rules The Syntax for the allowed operations 11

Faculty of Sciences and Social Sciences HOPE Stacks Operation LIFO 12 Last in First Out

Faculty of Sciences and Social Sciences HOPE Stacks Empty stack 13

Faculty of Sciences and Social Sciences HOPE Stacks Items added to stack 14 A B C D Always added to top of stack

Faculty of Sciences and Social Sciences HOPE Stacks Items removed from stack 15 A B C D Always removed from top of stack

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

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

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

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

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

Faculty of Sciences and Social Sciences HOPE Stacks Starting position 21 Temp

Faculty of Sciences and Social Sciences HOPE Stacks Get Stack4 22 Temp Stack4

Faculty of Sciences and Social Sciences HOPE Stacks Temp := A 23 Temp Stack4 A

Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 24 Temp Stack4 A A

Faculty of Sciences and Social Sciences HOPE Stacks Temp := B 25 Temp Stack4 B A

Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 26 Temp Stack4 B A B

Faculty of Sciences and Social Sciences HOPE Stacks Temp := C 27 Temp Stack4 B A C

Faculty of Sciences and Social Sciences HOPE Stacks Push(Stack4, Temp) 28 Temp Stack4 C A B C

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

Faculty of Sciences and Social Sciences HOPE Before … 30 A B C Stack1

Faculty of Sciences and Social Sciences HOPE After … 31 Stack1 C B A Stack2

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.

Faculty of Sciences and Social Sciences HOPE A Better Solution Would Be 33 Get Stack2 Reverse(Stack1, Stack2)

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)

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

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

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

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

Faculty of Sciences and Social Sciences HOPE 39 Do You Understand the Problem ?

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

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

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

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

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)

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

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

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

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.

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

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

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

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.

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 ???

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)

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

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.

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)

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 !

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)

Faculty of Sciences and Social Sciences HOPE More interesting stuff … 60

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

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

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

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

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

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

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

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

Faculty of Sciences and Social Sciences HOPE Another Boolean Function Light_On Returns True or False 69

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