Title: Structured Problem Solving
1StructuredProblem Solving
- Level C2005-2006
- George Herterich
2Counting Trace Tables
3Count 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
5Fred 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
6Total 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
7display Sum
Copies the contents of the variable Sum to the
default output device
156
Sum
156
8read(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
9Putting 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
10Now 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
11Now 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
12Now 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
13Now what ?
Brian
3
Current variable values
Lindy
19
Alma
0
3 19
display Brian , Lindy
14Good 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 ?
15Better 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).
16The 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
17Trace 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)
18The Empty Trace Table
Variables
19Here we go A 6
20Next StepB 8
21Next StepC A
22Next StepA B
23Next StepB C
24Tracing 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
25Here is the Blank Trace Table
Variables
26Here are the first 3 steps Initialisation
Variables
27Now the Test Is 0 lt 3 ?
Variables
YES
28Into the Loop A sequence of 3 steps
Variables
Assume 5 is input
15
01
29Back to the Test Step 4 Is 1 lt 3 ?
Variables
YES
30Into the Loop Again A sequence of 3 steps
Variables
Assume 2 is input
52
1 1
31The Test Again Step 4 Is 2 lt 3 ?
Variables
YES
32Into the Loop Again A sequence of 3 steps
Variables
Assume 4 is input
104
2 1
33Now the Test Step 4 Is 3 lt 3 ?
Variables
NO
34Thats the End of the Trace Table.
Last line of Program display Answer
Output the contents of variable Answer
40