Lecture 11 - Programming Fundamentals

You might also like

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

Lecture 11:

Hand Tracing of Programs

1
Lecture 11 Objectives

In this lecture, we will:

• Solve more hand tracing examples


Lecture 11 Contents

11.1 Introduction
11.2 Hand Tracing Examples
11.3 Summary
Lecture 11 11.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 11 Contents

11.1 Introduction
11.2 Hand Tracing Examples
11.3 Summary
Lecture 11 11.2 Hand Tracing Examples

Example 11.1:
#include <iostream>
using namespace std; A B C D
int main()
{
int A, B;
float C,D;
A = 2;
B = 5.0;
C = B / A;
D = (float) B/A;

cout<<"\nGiven a = 2, b = 5";
cout<<"\nc = "<<C;
cout<<"\nd = "<<D<<endl;
return 0;
}
Lecture 11 11.2 Hand Tracing Examples

Activity 11.1: Trace the following program.

#include <iostream>
using namespace std;
int main()
{
int Var1=5,Var2=3,Var3=5.5,Var4=3,C;
C=++Var1%Var2++; Var1 Var2 Var3 Var4 C
C=--Var1%Var2++;
C=Var3++%--Var4;
C=++Var3%Var4--;
return 0;
}
Lecture 11 11.2 Hand Tracing Examples

Activity 11.2: Trace the following program.


#include <iostream>
using namespace std;
int main()
{ A B Temp
int A,B,Temp;

A=5;
B=7;
Temp=A;
A=B;
B=Temp;
 
return 0;
}
Lecture 11 11.2 Hand Tracing Examples

Activity 11.3: Trace the following program.

#include <iostream>
using namespace std; n sum digit
int main()
{
int n = 1729, sum = 0, digit;

while (n > 0)
{
digit = n % 10;
sum = sum + digit;
n = n / 10;
}
cout << sum << endl; 
return 0;
}
Lecture 11 Contents

11.1 Introduction
11.2 Hand Tracing Examples
11.3 Summary
Lecture 11 11.3 Summary

• Hand Tracing helps us in finding logical errors in our


programs
QUIZ # 1
12
Quiz # 1
Time allowed: 10 minutes
• To compute and display of dot product of two vectors
in three-dimensional space, do the following:
– Identify the input(s) [1 Mark]
– Identify the output(s) [1 Mark]
– Identify the required control structure(s) [1 Mark]
– Write the Pseudo-code [3 Marks]
– Draw the Flowchart [4 Marks]

• Hint: A vector in 3D space can be expressed using its


three spatial coordinates i.e. x, y, and z components
13
Solution of Quiz # 1

14

You might also like