Copyright © 2004-2017 by Curt Hill Tracing What, How, and Why Copyright © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
Copyright © 2004-2017 by Curt Hill Elbonian Computers Copyright © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
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 ? 2 5 Copyright © 2004-2017 by Curt Hill
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 ? 2 5 7 2 5 Copyright © 2004-2017 by Curt Hill
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 ? 2 5 7 2 5 7 35 5 Copyright © 2004-2017 by Curt Hill
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 ? 2 5 7 2 5 7 35 5 7 35 14 Copyright © 2004-2017 by Curt Hill
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 ? 2 5 7 2 5 7 35 5 7 35 14 Copyright © 2004-2017 by Curt Hill
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 ? 2 5 7 2 5 7 35 5 7 35 14 Copyright © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill
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 © 2004-2017 by Curt Hill