Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213

Slides:



Advertisements
Similar presentations
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Advertisements

The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Recursively Defined Functions
Basics of Computer Programming Web Design Section 8-1.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Best-First Search: Agendas
Problem Solving and Program Planning
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 6: Problem Solving Exercises Stewart Blakeway FML.
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
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Examination Revision Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway
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
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Chapter 2: Design of Algorithms
Computer Science 1620 Programming & Problem Solving.
Logo Lesson 5 TBE Fall 2004 Farah Fisher. Prerequisites  Given a shape, use basic Logo commands and/or a procedure to draw the shape, with and.
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
Bug Session Two. Session description In this session the use of algorithms is reinforced to help pupils plan out what they will need to program on their.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Problem Solving and Algorithms
Invitation to Computer Science, Java Version, Second Edition.
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
By the end of this session you should be able to...
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Sight Words.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Algorithms and Pseudocode
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
An Introduction to Programming with C++1 An Introduction to Control Structures Tutorial 1.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Understand Problem Solving Tools to Design Programming Solutions
FLOWCHARTS Part 1.
Basics of Computer Programming
Understand Problem Solving Tools to Design Programming Solutions
Teaching design techniques to design efficient solutions to problems
Basics of Computer Programming
Basics of Computer Programming
Basics of Computer Programming
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Coding Concepts (Basics)
Algorithm Discovery and Design
Algorithm and Ambiguity
Flowcharting & Algorithms
Using a Stack Chapter 6 introduces the stack data type.
ICT Programming Lesson 3:
Using a Stack Chapter 6 introduces the stack data type.
Flowcharts and Pseudo Code
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213

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 2

Faculty of Sciences and Social Sciences HOPE What we shall do today Start writing out own algorithms Pages 36 to 50 3

Faculty of Sciences and Social Sciences HOPE Steps in Problem Solving Step 1 Establish the overall plan of action write down the solution as a single statement Step 2 Examine the statement to see if it could be expressed as one of the following: Sequence Selection Repetition 4

Faculty of Sciences and Social Sciences HOPE Steps in Problem Solving Step 3 Make it so. write it out as one of those Step 4 Repeat steps 2 and 3 for the new statements this generates Step 5 Stop when you are satisfied with the level of detail 5

Faculty of Sciences and Social Sciences HOPE Summary Step 1 Establish the overall plan of action write down the solution as a single statement Step 2 Examine the statement to see if it could be expressed as one of the following: Sequence Selection Repetition Step 3 Make it so. write it out as one of those Step 4 Repeat steps 2 and 3 for the new statements this generates Step 5 Stop when you are satisfied with the level of detail 6

Faculty of Sciences and Social Sciences HOPE Technical Phrases for this Technique Top Down Design Stepwise Refinement 7

Faculty of Sciences and Social Sciences HOPE Making sure we understand what the problem is we are trying to solve This conforms to obeying the first Law of Computing – Making sure we get paid – If we build what client wants then it is more likely we shall get paid 8

Faculty of Sciences and Social Sciences HOPE Making sure we understand what the problem is we are trying to solve Some Thoughts on Building a Dwelling 9

Faculty of Sciences and Social Sciences HOPE Some Thoughts on Building a Dwelling Initial ideas Type of House Size of House Location Now analyse each of the above 10

Faculty of Sciences and Social Sciences HOPE Type of House Bungalow Two Floors Three Floors Detached Terraced Semi-detached 11

Faculty of Sciences and Social Sciences HOPE Size of House How many bedrooms ? How many living rooms ? How many bathrooms ? 12

Faculty of Sciences and Social Sciences HOPE Location How many miles from shops ? Green belt ? North – South facing ? 13

Faculty of Sciences and Social Sciences HOPE Repeat this analysis until The level of detail is complete and unambiguous Front door exterior: Oxford blue gloss Main bathroom taps: gold plated And so-forth 14

Faculty of Sciences and Social Sciences HOPE An Example of Using Stepwise Refinement Eggs are stored on trays in a warehouse. The egg checker puts a tray on his work table. He then picks up each egg in turn. If it is cracked, he puts it into the blue box, otherwise he returns it to the tray. As he works through the eggs, he keeps count of the number cracked. When he has finished the tray, he reports how many eggs were cracked. 15

Faculty of Sciences and Social Sciences HOPE Summary Step 1 Establish the overall plan of action write down the solution as a single statement Step 2 Examine the statement to see if it could be expressed as one of the following: Sequence Selection Repetition Step 3 Make it so. write it out as one of those Step 4 Repeat steps 2 and 3 for the new statements this generates Step 5 Stop when you are satisfied with the level of detail 16

Faculty of Sciences and Social Sciences HOPE Step 1: Write down the solution as a single statement Deal with eggs 17

Faculty of Sciences and Social Sciences HOPE Step 2: Examine the statement to see if it could be expressed as sequence, selection or iteration Deal with eggs Sequence ? Selection ? Repetition ? 18 Is it a sequence of substeps? Is it a choice between alternatives? Is it a repeated action?

Faculty of Sciences and Social Sciences HOPE Step 2: Examine the statement to see if it could be expressed as sequence, selection or iteration Deal with eggs Sequence 19

Faculty of Sciences and Social Sciences HOPE Step 3: Make it so – a sequence 20 1.Put tray on table 2.Process eggs 3.Report number cracked First Refinement

Faculty of Sciences and Social Sciences HOPE Step 4: Repeat steps 2 and 3 for the new statements this generates 21

Faculty of Sciences and Social Sciences HOPE 1 Put Tray on Table 22 No further refinement necessary

Faculty of Sciences and Social Sciences HOPE 2 Process the Eggs 23 Treat this as a problem in its own right. Is there a repetition ?

Faculty of Sciences and Social Sciences HOPE 2 Process the Eggs 24 Treat this as a problem in its own right. Is there a repetition ? YES ! When does it stop ?

Faculty of Sciences and Social Sciences HOPE 2 Process the Eggs 25 Treat this as a problem in its own right. Is there a repetition ? YES ! When does it stop ?When he has processed all the eggs ! What does that mean ?

Faculty of Sciences and Social Sciences HOPE 2 Process the Eggs 26 Treat this as a problem in its own right. Is there a repetition ? YES ! When does it stop ?When he has processed all the eggs ! What does that mean ?When there are no more eggs to process

Faculty of Sciences and Social Sciences HOPE When There Are No More Eggs to Process 27 How can we rewrite this using the problem solving construct: while something is true begin do something end

Faculty of Sciences and Social Sciences HOPE while something is true 28 When do you stop ? When all the eggs have been examined ! What is the opposite of this ? Eggs remain to be examined ! This is the while test !

Faculty of Sciences and Social Sciences HOPE The repetition so far … 29 while eggs remain to be examined begin do something end

Faculty of Sciences and Social Sciences HOPE begin do something end 30 do something Keep it Simple deal with egg Not eggs

Faculty of Sciences and Social Sciences HOPE 3 Report Number Cracked 31 No further refinement yet.

Faculty of Sciences and Social Sciences HOPE Second Refinement Solution So Far 32 put tray on table while eggs remain to be examined begin deal with egg end report number cracked

Faculty of Sciences and Social Sciences HOPE deal with egg 33 Revisit the problem: How do we deal with egg ? Is it a sequence ?Yes ! Is it a repetition ?No ! Is it a selection ?Not Yet !

Faculty of Sciences and Social Sciences HOPE deal with egg 34 Sequence: pick up egg deal with cracked egg

Faculty of Sciences and Social Sciences HOPE pick up egg 35 End of story: level of detail fine: no further refinement needed.

Faculty of Sciences and Social Sciences HOPE deal with cracked egg 36 Revisit the problem: He then picks up each egg in turn. If it is cracked, he puts it into the blue box, otherwise he returns it to the tray. As he works through the eggs, he keeps count of the number cracked. Here is the selection mentioned earlier (the not yet ). Can we express this in the format: if condition then actions else other actions ?

Faculty of Sciences and Social Sciences HOPE if condition then…..else 37 if egg is cracked then begin put it in blue box add 1 to total of cracked eggs end else begin return egg to tray end

Faculty of Sciences and Social Sciences HOPE Solution So Far 38 put tray on table while eggs remain to be examined begin pick up egg if egg is cracked then begin put it in blue box add 1 to total of cracked eggs end else begin return egg to tray end report number cracked

Faculty of Sciences and Social Sciences HOPE Third Refinement 39 Let us use a variable to store the number of cracked eggs. Convention: Use a name which reflects the purpose of the variable Start with a capital letter No spaces If words are joined together make the first letter of each word a capital letter So in this case: NumberCracked

Faculty of Sciences and Social Sciences HOPE NumberCracked 40 Just as we would reset the odometer in our car to avoid doing mind boggling subtractions to find the length of a journey, we must set the initial value of NumberCracked to an appropriate value. Otherwise the final count could be anything. This is called initialisation and in this instance we would initialise the variable to ???????????? What ? How ?

Faculty of Sciences and Social Sciences HOPE Initialising the Variable 41 NumberCracked := 0

Faculty of Sciences and Social Sciences HOPE add 1 to total of cracked eggs 42 How ?

Faculty of Sciences and Social Sciences HOPE 43 NumberCracked := NumberCracked +1

Faculty of Sciences and Social Sciences HOPE The Solution Final refinement 44

Faculty of Sciences and Social Sciences HOPE 45 NumberCracked := 0 put tray on table while eggs remain to be examined begin pick up egg if egg is cracked then begin put it in blue box NumberCracked := NumberCracked + 1 end else begin return egg to tray end report NumberCracked

Faculty of Sciences and Social Sciences HOPE A Second Example of Using Stepwise Refinment 46 A parcel sorting office initially sorts the parcels by weight into three broad categories: light, medium and heavy. The parcels enter the weighing room on a circular conveyor belt, where they are taken off, weighed, weight stamped and placed in blue, red or green containers depending on whether they are light, medium or heavy respectively. When the conveyor belt stops, the containers are sent to the despatch office.

Faculty of Sciences and Social Sciences HOPE Step 1 47 Deal with parcels

Faculty of Sciences and Social Sciences HOPE Step 2 48 Deal with parcels Sequence ? Selection ? Repetition ?

Faculty of Sciences and Social Sciences HOPE Step 2 49 Deal with parcels Sequence

Faculty of Sciences and Social Sciences HOPE Step 3: Sequence - Make it so 50 1.Deal with parcels on conveyor belt 2.Send containers to the despatch office First Refinement

Faculty of Sciences and Social Sciences HOPE 1. Deal with parcels on conveyor belt 51 Revisit the problem: Is there repetition ?Yes ! When does it stop ?

Faculty of Sciences and Social Sciences HOPE 1. Deal with parcels on conveyor belt 52 Revisit the problem: Is there repetition ?Yes ! When does it stop ?When the conveyor belt stops. What is the while test ?

Faculty of Sciences and Social Sciences HOPE 1. Deal with parcels on conveyor belt 53 Revisit the problem: Is there repetition ?Yes ! When does it stop ?When the conveyor belt stops. What is the while test ?The opposite of this Conveyor belt running

Faculty of Sciences and Social Sciences HOPE 2. Send containers to despatch office 54 No further refinement needed.

Faculty of Sciences and Social Sciences HOPE Second Refinement 55 while conveyor belt running begin deal with parcel end send containers to the despatch office

Faculty of Sciences and Social Sciences HOPE deal with parcel 56 Sequence ?Yes ! Selection ?Not Yet ! Repetition ?No !

Faculty of Sciences and Social Sciences HOPE deal with parcel 57 Revisit the problem: Sequence: take parcel off conveyor belt weigh parcel stamp parcel with weight put in correct box

Faculty of Sciences and Social Sciences HOPE Third refinement 58 while conveyor belt running begin take parcel off conveyor belt weigh parcel stamp parcel with weight put in correct box end send containers to despatch office

Faculty of Sciences and Social Sciences HOPE Let’s go Further 59 take parcel off conveyor belt:no more detail needed weigh parcel :no more detail needed stamp parcel with weight:no more detail needed put in correct box:selection here ! Which box ?

Faculty of Sciences and Social Sciences HOPE Which Box ? 60 Revisit the Problem: if light parcelblue box if medium parcelred box if heavy parcelgreen box

Faculty of Sciences and Social Sciences HOPE The Final Algorithm 61

Faculty of Sciences and Social Sciences HOPE 62 while conveyor belt running begin take parcel off conveyor belt weigh parcel stamp parcel with weight if parcel is light then begin put in blue box end if parcel is medium then begin put in red box end if parcel is heavy then begin put in green box end send containers to despatch office