Download presentation
Presentation is loading. Please wait.
Published byPeter Freeman Modified over 9 years ago
1
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 3: Algorithms Stewart Blakeway FML 213 blakews@hope.ac.uk
2
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Aims of the presentation Algorithms and their representations – Pages 15 to 27
3
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What is Problem Solving?
4
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What is Problem Solving? Creating a solution to a problem
5
www.hope.ac.uk 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.
6
www.hope.ac.uk 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..
7
www.hope.ac.uk 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
8
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task
9
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task
10
www.hope.ac.uk 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
11
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side.
12
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side. Is this a good solution ?
13
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?
14
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution 2 Look right Look left Look right again Cross the road
15
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?
16
www.hope.ac.uk 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
17
www.hope.ac.uk 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
18
www.hope.ac.uk 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
19
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Nearly there ! Some fine tweaking needed !!
20
www.hope.ac.uk 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
21
www.hope.ac.uk 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
22
www.hope.ac.uk 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
23
www.hope.ac.uk 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
24
www.hope.ac.uk 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
25
www.hope.ac.uk 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
26
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE if inside a loop createpieeater(); randompies(20); while (clearahead) { if (pieinsight) { eatpie(); } else { walk(); }
27
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Problem I want a hot pie for lunch
28
www.hope.ac.uk 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
29
www.hope.ac.uk 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
30
www.hope.ac.uk 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
31
www.hope.ac.uk 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
32
www.hope.ac.uk 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
33
www.hope.ac.uk 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
34
www.hope.ac.uk 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
35
www.hope.ac.uk 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
36
www.hope.ac.uk 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
37
www.hope.ac.uk 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
38
www.hope.ac.uk 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
39
www.hope.ac.uk 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
40
www.hope.ac.uk 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
41
www.hope.ac.uk 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
42
www.hope.ac.uk 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
43
www.hope.ac.uk 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
44
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE selection with an else part Change of day
45
www.hope.ac.uk 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
46
www.hope.ac.uk 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
47
www.hope.ac.uk 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
48
www.hope.ac.uk 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
49
www.hope.ac.uk 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
50
www.hope.ac.uk 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
51
www.hope.ac.uk 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
52
www.hope.ac.uk 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
53
www.hope.ac.uk 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
54
www.hope.ac.uk 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
55
www.hope.ac.uk 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
56
www.hope.ac.uk 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
57
www.hope.ac.uk 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
58
www.hope.ac.uk 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 !!
59
www.hope.ac.uk 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
60
www.hope.ac.uk 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
61
www.hope.ac.uk 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
62
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Design tools for algorithms We have introduced two problem solving design tools: – Structured English – Flow charts
63
www.hope.ac.uk 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.