0% found this document useful (0 votes)
13 views

5.2 Trace Tables

The document covers the use of trace tables to analyze variable values during algorithm execution, emphasizing their role in identifying logical errors. It includes examples and class exercises to demonstrate how to apply trace tables for various variable scenarios. Additionally, it discusses operator precedence in mathematical expressions and provides exercises to calculate values based on given code snippets.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

5.2 Trace Tables

The document covers the use of trace tables to analyze variable values during algorithm execution, emphasizing their role in identifying logical errors. It includes examples and class exercises to demonstrate how to apply trace tables for various variable scenarios. Additionally, it discusses operator precedence in mathematical expressions and provides exercises to calculate values based on given code snippets.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Trace Tables, Message Box and Message Variable

LECTURE OBJECTIVES
 Be able to use Trace Tables to view variable values
 The message variable (string variable)
 Learn how to display output in a MessageBox
Trace Tables
 Trace tables can be used to test an algorithm to make
sure that the algorithm is free of logical errors

 A trace table usually takes the form of a multi-column,


multi-row table

 Each column represents a variable, and the rows are


used to show how the value of each variable changes
during the execution of the algorithm
Trace Tables – Example:
 Use a trace table to determine what the values of x, y,
and z will be once the code below has been executed.
Trace Tables – Example:
 Use a trace table to determine what the values of x, y,
and z will be once the code below has been executed.

x y z
5 3 4
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
7
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
7
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
7 4
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
7 4
Trace Tables – Example:
 Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.

x y z
5 3 4
7 4 11
Trace Tables - Class Exercise:
 Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.

a b c
0 1 5
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
1
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
1
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
1 7
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
1 7
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.

a b c
0 1 5
1 7 3
Order of Precedence
 Operator Precedence:
the rules that determine the order in which parts of a
mathematical expression are evaluated.

 Multiplication, division and remainder always take place


first prior to addition or subtraction

 Order of evaluation is always from left to right


answer = a + c * b + b / 2 - a;

Evaluation is from
left to right
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = (c * b) – a;

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = (c * b) – a;
20 = (3 * 7) - 1

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = c * b – a;

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = c * b – a;
20 = 3 * 7 - 1

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
What
will be once thewould the value
code below of executed.
has been answer
be?
ans = a + c * b + b / 2 - a;

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
ans= a + c * b + b / 2 - a;
24 = 1 + 3 * 7 + 7 / 2 – 1

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
What would the value of answer
 Use a trace table to determine what the values of a, b, and c
be?
will be once the code below has been executed.
ans= a + c * b + b / 2 - a;
24 = 1 + 3 * 7 + 7 / 2 – 1
24 = 1 + 21 + 3 - 1

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
answer = b * c / 2;

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
What would the value of answer
 Use a trace table to determine what the values of a, b, and c
be?
will be once the code below has been executed.
answer = b * c / 2;
10 = 7 * 3 / 2

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace table to determine what the values of a, b, and c
What
will be once thewould the has
code below value ofexecuted.
been answer
be?
answer = b / c * 2;

a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
 Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
answer = b / c * 2;
4 = 7 / 3 * 2

a b c
All variables are
0 1 5
integers
1 7 3
Trace Table Exercise
 Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.

int a = 10, b = 5, c = 3; a b c
a = b + c * 2;
b = b + (10 - c);
c = b + b;
Trace Table Solution
 Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
Trace Table Exercise
 Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
3 5 10

a = 3; b = 5; c = 10;
a = a * b;
b = c + 7 * a / 2;
a = b;
a = b / 10;
Trace Table Exercise
 Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
3 5 10
15 62
a = 3; b = 5; c = 10; 62
a = a * b; 6

b = c + 7 * a / 2;
a = b;
a = b / 10;

You might also like