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

Lecture 11 - Programming Fundamentals

This document summarizes a lecture on hand tracing programs. It has three main sections: 1. Introduction - Hand tracing involves stepping through a program statement-by-statement and recording the value of variables after each statement executes, to locate logic or math errors. 2. Examples - Three programming examples are given and students are asked to trace each by filling in a table with the variable values after each statement. The first prints values based on division calculations. The second swaps two variables' values. The third calculates a number's digit sum in a loop. 3. Summary - Hand tracing is recapped as a way to find logical errors in programs.

Uploaded by

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

Lecture 11 - Programming Fundamentals

This document summarizes a lecture on hand tracing programs. It has three main sections: 1. Introduction - Hand tracing involves stepping through a program statement-by-statement and recording the value of variables after each statement executes, to locate logic or math errors. 2. Examples - Three programming examples are given and students are asked to trace each by filling in a table with the variable values after each statement. The first prints values based on division calculations. The second swaps two variables' values. The third calculates a number's digit sum in a loop. 3. Summary - Hand tracing is recapped as a way to find logical errors in programs.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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