Structured Problem Solving - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Structured Problem Solving

Description:

(this has entirely different connotations if 'variable' is an adjective: ... Steps are numbered wherever variable contents are changed or conditions are tested. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 35
Provided by: hert4
Category:

less

Transcript and Presenter's Notes

Title: Structured Problem Solving


1
StructuredProblem Solving
  • Level C2005-2006
  • George Herterich

2
Counting Trace Tables
  • Pages 28 to 35

3
Count 0
  • This does 2 things
  • Establishes an area of RAM called Count
  • Puts zero into this location

?????
Count
0
Count
4
  • This can be read as
  • becomes
  • or
  • becomes equal to

5
Fred 12
???????
1
Fred
Fred
12
2
There is no significance in the name of the
variable It is not a keyword It is up to the
programmer
6
Total 0Total Total 1
These 2 lines of code do 3 things.
?????
Total
Set up the variable Total
1
0
Total
Initialise it to zero
2
The new contents of Total become equal to the old
contents plus 1
3
1
Total
7
display Sum
Copies the contents of the variable Sum to the
default output device
156
Sum
156
8
read(Fred)
This does three things
???
Fred
1
Establishes a variable called Fred
Waits for input from the input device For
example key press 8
2
8
Fred
3
Places 8 into the variable Fred
9
Putting it altogether so far
Brian
0
Pre-Read
Brian 0 Lindy 0 read(Alma) while Alma
greater than 0 begin Brian Brian 1 Lindy
Lindy Alma read(Alma) end display Brian , Lindy
Lindy
0
Alma
6
Assume that 6 is input
Brian
1
Lindy
6
Assume that 9 is input
9
Alma
Back to the while
Post-read
10
Now what ?
Brian
Current variable values
1
Lindy
6
while Alma greater than 0 begin Brian Brian
1 Lindy Lindy Alma read(Alma) end display
Brian , Lindy
9
Alma
Brian
2
Lindy
15
Alma
4
Assume that 4 is input
11
Now what ?
Brian
Current variable values
2
Lindy
15
while Alma greater than 0 begin Brian Brian
1 Lindy Lindy Alma read(Alma) end display
Brian , Lindy
Alma
4
Brian
3
Lindy
19
Alma
0
Assume that 0 is input
12
Now what ?
Brian
3
Current variable values
Lindy
19
while Alma greater than 0 begin Brian Brian
1 Lindy Lindy Alma read(Alma) end display
Brian , Lindy
Alma
0
The test now fails returns false
Control now jumps to the line after the end of
the while loop
13
Now what ?
Brian
3
Current variable values
Lindy
19
Alma
0
3 19
display Brian , Lindy
14
Good Practice Variable Names
Variable names should reflect their intended
purpose. This purpose, like a well designed user
interface, should be guessable. This helps with
the readability of a program, together with
comments.
What is the intended purpose of each of the
variables Brian, Lindy and Alma ?
15
Better Variable Names
1 is added to the contents of Brian (initially
zero) each time through the while loop. Each
number input is stored (not added to) in the
variable Alma. (this has entirely different
connotations if variable is an adjective
Georges joke). The value stored in Alma is added
to the current value of Lindy (initially zero).
16
The Names at Last
Brian seems to be counting each loop. Alma seems
to be storing each number input Lindy seems to be
keeping a running total. Brian
Count Alma
Number Lindy Total Refer
to your course booklet on page 24
17
Trace Tables
The above method of keeping track of the contents
of variables is obviously tedious. A better way
would be to employ a tabular method. This is
called a trace table. Consider the following
algorithm A 6 (Step 1) B 8 (Step 2) C
A (Step 3) A B (Step 4) B C (Step 5)
18
The Empty Trace Table
Variables
19
Here we go A 6
20
Next StepB 8
21
Next StepC A
22
Next StepA B
23
Next StepB C
24
Tracing a Loop
Answer 1 (Step 1) I 0 (Step
2) Number 0 (Step 3) while (I is less than
3 ) (Step 4) begin read(Number) (Step
5) Answer Answer Number (Step 6) I I
1 (Step 7) end display Answer
Steps are numbered wherever variable contents are
changed or conditions are tested.
Multiply
25
Here is the Blank Trace Table
Variables
26
Here are the first 3 steps Initialisation
Variables
27
Now the Test Is 0 lt 3 ?
Variables
YES
28
Into the Loop A sequence of 3 steps
Variables
Assume 5 is input
15
01
29
Back to the Test Step 4 Is 1 lt 3 ?
Variables
YES
30
Into the Loop Again A sequence of 3 steps
Variables
Assume 2 is input
52
1 1
31
The Test Again Step 4 Is 2 lt 3 ?
Variables
YES
32
Into the Loop Again A sequence of 3 steps
Variables
Assume 4 is input
104
2 1
33
Now the Test Step 4 Is 3 lt 3 ?
Variables
NO
34
Thats the End of the Trace Table.
Last line of Program display Answer
Output the contents of variable Answer
40
Write a Comment
User Comments (0)
About PowerShow.com