Structured Problem Solving - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Structured Problem Solving

Description:

We are heading towards learning how to program a computer. ... All of the algorithms developed so far have been fairly lax in the choice of phraseology ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 40
Provided by: hert4
Category:

less

Transcript and Presenter's Notes

Title: Structured Problem Solving


1
StructuredProblem Solving
  • Level C
  • 2006-2007
  • George Herterich

2
Lets Tighten up a Little
  • We are heading towards learning how to program a
    computer.
  • Compooters are knott intellygent lyke wot we
    rrrr.
  • Computer programming languages have strict rules
    of grammar.
  • This set of rules constitutes the syntax of that
    particular language

Pages 51 to 60
3
Lets 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
Lets Tighten up a Little
  • A bridge between algorithmic design and actual
    computer programs which run correctly
  • Specify the rules
  • Specify the syntax
  • Specify the problem
  • Test the solution

5
This Will Certainly Help
6
StacksA Specification
  • The Environment
  • The Rules
  • The Syntax for the allowed operations

7
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.
  • A tray which can hold 1 book only.
  • (This is labelled Temp)

8
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

9
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 Stackn

10
Specify the Problem
  • Generate a Stack of books, labelled Stack2, which
    are in the reverse order to those in Stack1

11
Top Level Solution
Reverse(Stack1, Stack2) This could be a shorthand
way of specifying the problem Remember Stack1 is
given, but Stack2 is still stored away.
12
A Better Solution Would Be
Get Stack2 Reverse(Stack1, Stack2)
13
Reverse(Stack1, Stack2)
Let us do it manually using books and boxes or
the equivalent substitutes. This will give a feel
for any sequence, selection or repetition
14
Next Level Solution
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)
15
The Algorithm So Far
Get Stack2 while Stack1 not empty begin move a
book from Stack1 to Stack2 end
16
move a book from Stack1 to Stack2
We must follow the rules of the game. How did we
do it manually? Stack1 Tray Stack2
Pop
Push
17
The Solution
Get Stack2 while Stack1 not empty begin
Pop(Stack1, Temp) Push(Temp, Stack2) end
Test the solution This can be done manually
18
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

19
Do You Understand the Problem ?
20
Understanding the Problem
Before the Merge
21
Understanding the Problem
After the Merge
22
Top Level Solution
Obvious from previous example
Get Stack3 Merge(Stack1, Stack2, Stack3)
Possible way of writing problem
23
Top Level Solution
Get Stack3 No more refinement
needed Merge(Stack1, Stack2, Stack3) Try it
manually as before It seems to be a repetition of
Stack1
First
Second
Tray
Stack3
Stack2
Third
24
A Repetition of What ?
A repetition of Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3)
25
We Now Have
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

26
What is the Problem ?
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
27
Let us Focus on the while Test
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
28
Seems EasyBut What is the Problem ?
Unknown starting states, as before. Let us look
at some possible solutions and test them.
29
Solution A
Get Stack3 while EITHER Stack1 OR Stack2 is not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
Test this solution for 2 or 3 different pairs of
stacks of different sizes. Revise the OR truth
table
30
Solution B
Get Stack3 while BOTH Stack1 AND Stack2 are not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
Test this solution for 2 or 3 different pairs of
stacks of different sizes. Revise the AND truth
table
31
Solution C
Get Stack3 while BOTH Stack1 AND Stack2 are not
empty begin Pop(Stack1, Temp) Push(Temp,
Stack3) Pop(Stack2, Temp) Push(Temp, Stack3) end
At this stage, one of the stacks is empty, but
we do not know which !!!!
32
Solution C Continued
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.
33
We have certainly merged the two Stacks, but the
resulting Stack, Stack3, does not quite fit the
problem specification. Why Not ???
34
Very Nearly There
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 a
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)
35
At LastThe Solution
Get Stack3 Get Stack4 merge(Stack1, Stack2,
Stack4) reverse(Stack4, Stack3)
Procedure Calls
Parameters
36
Question
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.
37
Solution
Get Stackw Get Stackm Get Stackn merge(Stackx,
Stacky, Stackn) reverse(Stackn,
Stackw) merge(Stackw, Stackz, Stackm) reverse(
Stackm, Stackn)
38
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 !
39
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)
Write a Comment
User Comments (0)
About PowerShow.com