www.hope.ac.uk faculty of sciences and social sciences hope variables and trace tables stewart...
Post on 19-Dec-2015
216 views
TRANSCRIPT
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables and Trace Tables
Stewart Blakeway
FML 213
http://hopelive.hope.ac.uk/computing/
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we have done so far
• Introduced Java as a programming language with three constructs:– Sequence– Selection– Repetition
• Introduced algorithms expressed in:– Structured English– Flow charts
2
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we shall do today
• Introduce the concept of a variable• Apply variables to algorithms• Introduce the trace table as a method of
checking the operation of a program by monitoring its variables
3
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Counting & Trace Tables
• Pages 29 to 36
4
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any job we do requires short term memory
• Coming to class– Where is it?– What time does it start?
• Getting a round in– Who ordered what?
5
What time is it ?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Some things we remember have to be continually updated
• Following a recipe– Where are we up to?
• Counting people coming into a room– What was the last count?
• Watching a game and you can’t see the scoreboard– What score is it?
6
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question:• How do we record what we need to
remember in algorithms ?
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question:• How do we record what we need to
remember in algorithms ?
8
Answer:Use
variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables
• A variable is somewhere in memory you store a piece of information you want to remember
9
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables
• A variable is somewhere in memory you store a piece of information you want to remember
• As you work through a task, that piece of information stored in memory may get
updated – its contents vary over time
10
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Representing a variable
11
14Number of people in the room
2-1Current score
• A box represents an area of memory
• The box is labelled
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Rules for naming variables
12
14NumberOfPeopleInRoom
2-1CurrentScore
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Rules for naming variables
• No spaces in the name• Use capitals to make name more readable• Preparing for converting to a computer
programming language where normally no spaces are allowed in variable names
13
14NumberOfPeopleInRoom
2-1CurrentScore
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
14
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
15
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
NumberOfPeopleInRoom = 25;
CurrentScore = "2-1";
16
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in algorithms
NumberOfPeopleInRoom := 25
CurrentScore := “2-1”
17
Making the variable and putting in the first value are done in one step
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Count := 0
This does 2 things
1. Establishes an area of memory called Count
2. Puts zero into this location
18
?????Count
0Count
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• This can be read as
– becomes
or –becomes equal to
19
:=
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Fred := 12
20
???????1 Fred
2 12Fred
There is no significance in the name of the variable
It is not a keyword like while, if, …
It is up to the programmer
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Total := 0Total := Total + 1
21
These 2 lines do 3 things.
1
2
3
?????Total
0
1
Total
Total
Set up the variable Total
Initialise it to zero
The new contents of Total become equal to the old
contents plus 1
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
display Sum
22
156Sum
156
Copies the contents of the variable Sum to the default output device
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
read(Fred)
23
This does three things
1
2
3
Establishes a variable called Fred
???Fred
Waits for input from the input device:
For example key press 8
Places 8 into the variable Fred
8Fred
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Reading input in Java
int Fred;
System.in.read(Fred);
24
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
25
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
26
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
?
?
Amanda
Brian?
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
0
?
Amanda
Brian?
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
28
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
0
?
Amanda
Brian0
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
29
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
Assume that 6 is input
0
6
Amanda
Brian0
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
30
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
Alma is greater than 0
so we go into loop body
0
6
Amanda
Brian0
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
31
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
6
Amanda
Brian0
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
32
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
6
Amanda
Brian6
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
33
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
4
Amanda
Brian6
Alma
Variables
Assume that 4 is input
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
34
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
4
Amanda
Brian6
Alma
Variables
Alma is greater than 0
so we go into loop body
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
35
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
4
Amanda
Brian6
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
36
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
4
Amanda
Brian10
Alma
Variables
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
37
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
Assume that 0 is input
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
38
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
Alma is no longer greater than 0
so the loop is finished
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
39
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
2
10
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java versionint Brian;int Stewart;int Alma;
Brian = 0;Stewart = 0;System.in.read(Alma);while (Alma > 0) { Brian = Brian + 1; Stewart = Stewart + Alma; System.in.read(Alma); }System.out.println(Brian , Stewart);
40
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Good Practice: Variable Names
41
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, Stewart and Alma ?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Better Variable Names
42
1 is added to the contents of Brian (initially zero) each time through the while loop.
Each number input is stored in (not added to) the variable Alma.
The value stored in Alma is added to the current value of Stewart (initially zero).
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
The Names at Last
43
Brian seems to be counting each loop.
Alma seems to be storing each number input
Stewart seems to be keeping a running total.
Brian Count
Alma Number
Stewart Total
Refer to your course booklet on page 31
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Trace Tables
44
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)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
The Empty Trace Table
45
Step Number
A B C
123
4
5
Variables
A := 6B := 8C := AA := BB := C
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Here we go
46
Step Number
A B C
1 623
4
5
A := 6B := 8C := AA := BB := C
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
47
Step Number
A B C
1 62 83
4
5
A := 6B := 8C := AA := BB := C
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
48
Step Number
A B C
1 62 83 6
4
5
A := 6B := 8C := AA := BB := C
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
49
A := 6B := 8C := AA := BB := C
Step Number
A B C
1 6
2 8
3 6
4 8
5
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
50
A := 6B := 8C := AA := BB := C
Step Number
A B C
1 6
2 8
3 6
4 8
5 6
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Tracing a Loop
51
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
Multiply
Steps are numbered wherever variable contents are changed or conditions are tested.
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Here is the Blank Trace Table Step
NumberAnswe
rI Numbe
rIs I < 3 ?
52
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
53
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
54
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
55
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Now the Test: Is 0 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
56
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
57
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 5 is input
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
58
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
5 times 1 = 5
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
59
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
1 is added to I
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 1 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
60
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
61
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 2 is input
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
62
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
5 times 2 = 10
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
63
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
1 is added to I
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 2 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
64
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
65
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 4 is input
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
66
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
10 times 4 = 40
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
67
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Add 1 to I
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 3 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
4 False68
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 3 < 3 ? Step
NumberAnswe
rI Numbe
rIs I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
4 False69
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
That’s the End of the Trace Table.
70
Last line of Program: display Answer
40Output the contents of variable Answer
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java Versionint Answer;int Number;int I;
Answer = 1;I = 0;Number = 0;while (I < 3) { System.in.read(Number); Answer = Answer * Number; I = I + 1; }System.out.println(Answer);
71
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next?• Assessment due – this has been emailed to
you.
if (!receivedAssessment)
begin
email [email protected]
end
else
begin
do assessment
end