Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Lecture 10:

Hand Tracing of Programs

1
Lecture 10 Objectives

In this lecture, we will learn about:

• Types of errors during program execution

• Hand tracing as a debugging technique


Lecture 10 Contents

10.1 Introduction
10.2 Types of Errors
10.3 Hand Tracing a Program
10.4 Summary
Lecture 10 10.1 Introduction
Introduction
• Hand tracing of programs:
• As if you are the computer, executing a program:
• step through and ‘execute’ each statement,
• one-by-one record the contents of all variables after
statement execution,
• using a hand trace chart (table)

• Useful to locate logic or mathematical errors !


Lecture 10 Contents

10.1 Introduction
10.2 Types of Errors
10.3 Hand Tracing a Program
10.4 Summary
Lecture 10 10.2 Types of Errors
Types of Errors

• Types of errors during program execution


• Syntax errors
• Logical errors
• Syntax Errors
• Those errors which occur because the rules of a
programming language are violated
• Compiler catches such errors
• Logical Errors:
• Errors in the program logic
• Cannot be caught by the compiler
Lecture 10 10.2 Types of Errors
Types of Errors

• Common syntax errors include:


• Different case of variable names: F vs f, X1 vs x1
• Incorrect spelling of variable names: Fahrenheit vs. Fahreheit
• Different case of keywords: Int vs int, Float vs float
• Incorrect spelling of keywords: cout vs count
• Missing required punctuations: missing semicolon (;) or << or
incorrectly terminated strings “”
• Common logical errors include:
• Incorrect formula due to e.g. missing or incorrectly placed
parenthesis
• Incorrect data type used e.g. int instead of float
• Incorrect order of statements
Lecture 10 10.2 Types of Errors
How to find logical errors?

• Debugging:
• Can be used within IDE (more during lab)
• Hand tracing of programs using pen and paper
Lecture 10 Contents

10.1 Introduction
10.2 Types of Errors
10.3 Hand Tracing a Program
10.4 Summary
Lecture 10 10.3 Hand Tracing A Program

Example:
int main ()
{ num1 num2 num3 avg
float num1,num2,num3,avg;

cout<<"Enter the first number: \n";


cin>>num1;

cout<<"Enter the second number: \n";


cin>>num2;

cout<<"Enter the third number: \n");


cin>>num3;

avg=(num1+num2+num3)/3;

cout<<"The average is “<<avg;


return 0;
}
Lecture 10 10.3 Hand Tracing A Program

Example:
int main ()
{ num1 num2 num3 avg
float num1,num2,num3,avg;
? ? ? ?
cout<<"Enter the first number: \n"; 1.1 ? ? ?
cin>>num1;
1.1 2.2 ? ?
cout<<"Enter the second number: \n";
cin>>num2; 1.1 2.2 3.3 ?

cout<<"Enter the third number: \n"); 1.1 2.2 3.3 2.2


cin>>num3;

avg=(num1+num2+num3)/3;

cout<<"The average is “<<avg;


return 0;
} The average is 2.2
Lecture 10 10.3 Hand Tracing A Program

Activity 10.1: Trace the following program.

int main()
{
int x, y, z;
x =10; y = 17;
z = x + y;
y = y - x;

cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;


x = y * z;
z = x / 20;
y = z % x;

cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;


return 0;
}
Lecture 10 10.3 Hand Tracing A Program
x y z
Solution 10.1:

int main()
{
int x, y, z;
x =10; y = 17;
z = x + y;
y = y - x;

cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;


x = y * z;
z = x / 20;
y = z % x;

cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;


return 0;
}
Lecture 10 10.3 Hand Tracing A Program
x y z
Solution 10.1:
? ? ?
10 17 ?
10 17 27
int main()
{ 10 7 27
int x, y, z; 189 7 27
x =10; y = 17;
z = x + y; 189 7 9
y = y - x; 189 9 9
cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;
x = y * z;
z = x / 20;
y = z % x;

cout<<"x: “<<x<<“y: ”<<y<<“z: ”<<z;


return 0;
}
Lecture 10 10.3 Hand Tracing A Program

Activity 10.2: Trace the following program.


int main()
{
int n, m, x, y;
m=10;
n=m*2/(m+2);
m%=n+2;

cout<<"n: “<<n;
cout<<"\nm: “<<m;
x=4;
y=x*2+10%3-1*x;
x*=y/m;

cout<<"\ny: “<<y;
cout"\nx: “<<x;
return 0;
}
Lecture 10 10.3 Hand Tracing A Program
n m x y
Solution 10.2:

int main()
{
int n, m, x, y;
m=10;
n=m*2/(m+2);
m%=n+2;

cout<<"n: “<<n;
cout<<"\nm: “<<m;
x=4;
y=x*2+10%3-1*x;
x*=y/m;

cout<<"\ny: “<<y;
cout<<"\nx: “<<x;

return 0;
}
Lecture 10 10.3 Hand Tracing A Program
n m x y
Solution 10.2:
? ? ? ?
int main() ? 10 ? ?
{
int n, m, x, y; 1 10 ? ?
m=10; 1 1 ? ?
n=m*2/(m+2);
m%=n+2; 1 1 4 ?
1 1 4 5
cout<<"n: “<<n;
cout<<"\nm: “<<m; 1 1 20 5
x=4;
y=x*2+10%3-1*x;
x*=y/m;

cout<<"\ny: “<<y;
cout<<"\nx: “<<x;

return 0;
}
Lecture 10 Contents

10.1 Introduction
10.2 Types of Errors
10.3 Hand Tracing a Program
10.4 Summary
Lecture 10 10.4 Summary

• Hand Tracing helps us in finding logical errors in our


programs

• The syntax errors are found by the compiler

You might also like