Download presentation
Presentation is loading. Please wait.
1
Copyright © 2004-2017 by Curt Hill
Tracing What, How, and Why Copyright © by Curt Hill
2
Copyright © 2004-2017 by Curt Hill
What? Tracing is the simulation of a computer program or part of a computer program We “play computer” The results should be the same as if the program were executed on a real machine The simulation needs to cover every relevant aspect of the computation Copyright © by Curt Hill
3
Copyright © 2004-2017 by Curt Hill
Elbonian Computers Copyright © by Curt Hill
4
Copyright © 2004-2017 by Curt Hill
Why? Essential debugging tool Important learning technique A correct trace demonstrates that you understand the fundamentals We will have tracing exercises in class and on tests Copyright © by Curt Hill
5
Copyright © 2004-2017 by Curt Hill
How? Find the first statement to be executed Write down all the variables Used to record their current value As new variables are encountered they may be added to the list For each statement that is encountered: Determine its action Record changes to any variables Show the final values when execution is complete Copyright © by Curt Hill
6
Consider the following code:
#include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c c = b/2 – c + 2 cout << a << “ “ << b << “ “ << c; return 0; } Copyright © by Curt Hill
7
Copyright © 2004-2017 by Curt Hill
Now what? What is the first executable statement? The declaration Record the variables and their value Then step through the statements Copyright © by Curt Hill
8
Copyright © 2004-2017 by Curt Hill
First step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c c = b/2 – c + 2 cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
9
Copyright © 2004-2017 by Curt Hill
Second step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c c = b/2 – c + 2 cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
10
Copyright © 2004-2017 by Curt Hill
Third step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c; c = b/2 – c + 2; cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
11
Copyright © 2004-2017 by Curt Hill
Fourth step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c; c = b/2 – c + 2; cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
12
Copyright © 2004-2017 by Curt Hill
Fifth step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c; c = b/2 – c + 2; cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
13
Copyright © 2004-2017 by Curt Hill
Sixth step: #include <iostream.h> int main(){ int a,b=2,c=5; cin >> a; b = a*c; c = b/2 – c + 2; cout << a << “ “ << b << “ “ << c; return 0; } a b c ? Copyright © by Curt Hill
14
Copyright © 2004-2017 by Curt Hill
Right and Wrong There is a right way and a wrong way to do tracing The right way is this The wrong way is to attempt to analyze this algebraically That approach does not allow for a change in variables Copyright © by Curt Hill
15
Copyright © 2004-2017 by Curt Hill
Conclusion Tracing is something that you see in every test and many quizzes and group assignments It is very useful to determine if you understand what is happening A very good debugging tool as well Copyright © by Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.