Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 3: Algorithms Stewart Blakeway FML 213

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Algorithms 10 IST – Topic 6.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
PSEUDOCODE & FLOW CHART
Basics of Computer Programming Web Design Section 8-1.
 Control structures  Algorithm & flowchart  If statements  While statements.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
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 6: Problem Solving Exercises Stewart Blakeway FML.
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 Java: Loops within loops Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 3 Planning Your Solution
The Program Design Phases
Review Algorithm Analysis Problem Solving Space Complexity
ALGORITHMS AND FLOWCHARTS
Lecture # 8 ALGORITHMS AND FLOWCHARTS. Algorithms The central concept underlying all computation is that of the algorithm ◦ An algorithm is a step-by-step.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
The Logical Structure of Algorithms. Algorithms Steps necessary to complete a task or solve a problem. Example: Recipe for baking a cake –Will contain.
End Show Writing a computer program involves performing the following tasks. 1. Understanding the problem 2. Developing an Algorithm for the problem 3.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Chapter 5 Algorithms Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information Technology.
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.
Introduction to Algorithms Engineering 1D04, Teaching Session 1.
ALGORITHM CHAPTER 8. Chapter Outlines and Objectives  Define an algorithm and relate it to problem solving.  Define three construct and describe their.
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.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Computer Programming I Summer 2011
Structured Programming (4 Credits)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
IPC144 Session 4 Flowcharts 1. Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify.
Algorithms and Pseudocode
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
ICS124 Session 9 Flowcharting 1. By the end of this section the student will be able to:  Name the three structures of the Structure Theorem  Identify.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to Flowcharts
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Understand Problem Solving Tools to Design Programming Solutions
Basics of Computer Programming
Algorithms and Flowcharts
Understand Problem Solving Tools to Design Programming Solutions
Introduction To Flowcharting
Basics of Computer Programming
Introduction to Algorithm – part 1
Basics of Computer Programming
Basics of Computer Programming
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Problem-Solving and Control Structures By Faith Brenner
Exploring Computer Science Lesson 6-2
MSIS 655 Advanced Business Applications Programming
ALGORITHMS AND FLOWCHARTS
Introduction to Algorithms and Programming
Problem Solving Designing Algorithms.
Problem-Solving and Control Structures By Faith Brenner
Flowcharts and Pseudo Code
ICT Gaming Lesson 2.
WJEC GCSE Computer Science
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213

Faculty of Sciences and Social Sciences HOPE Aims of the presentation Algorithms and their representations – Pages 15 to 27

Faculty of Sciences and Social Sciences HOPE What is Problem Solving?

Faculty of Sciences and Social Sciences HOPE What is Problem Solving? Creating a solution to a problem

Faculty of Sciences and Social Sciences HOPE What is Problem Solving? Creating a solution to a problem A solution to a problem is a set of instructions that, if carried out, will lead to a successful conclusion.

Faculty of Sciences and Social Sciences HOPE What is Problem Solving? A set of instructions that, if carried out, will lead to a successful conclusion is known as..

Faculty of Sciences and Social Sciences HOPE What is Problem Solving? A set of instructions that, if carried out, will lead to a successful conclusion is known as..... an algorithm

Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task

Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task

Faculty of Sciences and Social Sciences HOPE Here’s a Problem Crossing a Busy Main Road !!!! Write down a set of instructions, using short phrases, to solve the above problem. Work in pairs or groups of three

Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side.

Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side. Is this a good solution ?

Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?

Faculty of Sciences and Social Sciences HOPE Solution 2 Look right Look left Look right again Cross the road

Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?

Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road

Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road We are using a specialised form of English to show repetition. This form of English is called: Structured English

Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road Structured English is also known as: Program Design Language PDL Pseudocode

Faculty of Sciences and Social Sciences HOPE Nearly there ! Some fine tweaking needed !!

Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road

Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross

Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross

Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross

Faculty of Sciences and Social Sciences HOPE Problem Solving Constructs A famous computer scientist called Dijkstra showed that solutions to any problem can be described in term of the following 3 constructs Sequence Selection Repetition

Faculty of Sciences and Social Sciences HOPE Problem Solving Constructs A famous computer scientist called Dijkstra showed that solutions to any problem can be described in term of the following 3 constructs Sequence Selection Repetition We met these in Java Trainer

Faculty of Sciences and Social Sciences HOPE if inside a loop createpieeater(); randompies(20); while (clearahead) { if (pieinsight) { eatpie(); } else { walk(); }

Faculty of Sciences and Social Sciences HOPE Problem I want a hot pie for lunch

Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end

Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end Structured English can show: Sequence Selection Repetition

Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end Structured English can show: Sequence Selection Repetition Sequence Selection Sequence Repetition

Faculty of Sciences and Social Sciences HOPE Algorithms in Structured English The solutions to problems written in the above style using keywords such as: – if …then …else – while – begin … end are algorithms written in Structured English

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do the second thing do the third thing Start Finish Here is a pictorial representation of the algorithm. This kind of pictorial representation is called a flow chart. Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do the second thing do the third thing Start Finish Each step is inside a box. The algorithm starts at the bubble labelled Start and ends at the bubble labelled Finish. The action follows the trail of arrows. Each step is inside a box. The algorithm starts at the bubble labelled Start and ends at the bubble labelled Finish. The action follows the trail of arrows. Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm with selection: do first thing if today is Thursday then begin do this thing end do last thing

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm with selection: do first thing if today is Thursday then begin do this thing end do last thing Note the indentation. This is essential if the reader is to see easily what happens if today is Thursday. Without indentation, the algorithm's structure is less clear: do first thing if today is Thursday then begin do this thing end do last thing Note the indentation. This is essential if the reader is to see easily what happens if today is Thursday. Without indentation, the algorithm's structure is less clear: do first thing if today is Thursday then begin do this thing end do last thing

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do this thing do the last thing Start Finish today is Thursday ? True False Again, each step is inside a box. One of them is diamond shaped. In addition, between the if and then keywords in the algorithm is the if condition that can be true or false. In this case whether or not it is Thursday today. Again, each step is inside a box. One of them is diamond shaped. In addition, between the if and then keywords in the algorithm is the if condition that can be true or false. In this case whether or not it is Thursday today. Here is the corresponding flow chart

Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do this thing do the last thing Start Finish today is Thursday ? True False In the flow chart, the if condition is represented by the diamond box with two alternative routes out. If the condition is true then the True route is followed, otherwise the False route is followed. In the flow chart, the if condition is represented by the diamond box with two alternative routes out. If the condition is true then the True route is followed, otherwise the False route is followed. Here is the corresponding flow chart

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Change of day

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept

Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart

Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Spot the 3 constructs !!

Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Spot the 3 constructs !! Structured English

Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Draw the flow chart

Faculty of Sciences and Social Sciences HOPE put egg in fridge True open the box of eggs take out an egg throw away the box StartFinish there are eggs in box ? False open the fridge door close the fridge door True egg is cracked ? False put egg in mother-in-law's shopping bag

Faculty of Sciences and Social Sciences HOPE Design tools for algorithms We have introduced two problem solving design tools: – Structured English – Flow charts

Faculty of Sciences and Social Sciences HOPE Next? Your first assessment is set in the seminar Eddie Izzard’s printing problem (web)web (flv)flv