Lecture 17: Modular Programming (cont) Debugging and Debuggers.
-
Upload
hilary-hines -
Category
Documents
-
view
235 -
download
0
Transcript of Lecture 17: Modular Programming (cont) Debugging and Debuggers.
Lecture 17:Modular Programming (cont)
Debugging and Debuggers
2
Lecture Contents:
Debugging and errors Classification of errors:
– Syntax errors;– Run-time errors;– Undetected errors;– Logic errors.
A program with multiple functions
3
Debugging and Debuggers
Debugging – removing errors from a program
Debugger – an utility program that helps the developer to register, detect, localize and repair errors.
IDE support built-in debugger: – Trace mode of execution– Watch variable values during execution, etc.
4
Errors classification
Syntax errors – detected at compile time:– missing semicolon,– undeclared identifier, …
Run time errors – detected at run time:– a=b/c; – division by zero error
Undetected errors:– mixture of character and numeric data at input,– uninitialized variable int sum; sum = sum + 30;
Logic errors – faulty or incorrect algorithm
5
5.9 Debugging and Testing Programs
Modern Integrated Development Environments (IDEs) include features to help you debug a program while it is executing.
If you cannot use a debugger, insert extra diagnostic output statements to display intermediate results at critical points in your program.
6
MS Visual C++ Debugger
How to use?Create breakpoint (click on a source text line, select
Debug->Toggle Breakpoint, or F9, red marker appears to the left of the source line)
To start debugger: Debug->Start Debugging or F5; Program execution suspended at a break point. Yellow arrow appears to the left of the statement prior to be executed over the red marker. Autos or Locals or Threads or Modules or Watch panes get opened.
To trace execution press: F10 Step Over, F11 Step Into, Shift+F11 Step out. For more info, see coming slides.
7
MS Visual C++ Debugger
8
MS Visual C++ Debugger
9
MS Visual C++ Debugger
10
MS Visual C++ Debugger
11
MS Visual C++ Debugger
12
MS Visual C++ Debugger
13
MS Visual C++ Debugger
14
6.5 Debugging and Testing a Program System
Top-Down testing and use of Stubs– Large projects– Stubs for all functions not finished (substitute
for a specific function) just a heading without any details other than some type of message
Bottom-Up testing and use of Drivers– Driver used by developer to test full
functionality of their function
15
Debugging and Testing a Program System
Debugging Tips for Program Systems– Carefully document each function parameter and local variable using comments as you write the code. Also describe the function’s purpose using comments.
– Create a trace of execution by displaying the function name as you enter it.
– Trace or display the values of all input and input/output parameters upon entry to a function. Check that these values make sense.
16
Debugging and Testing a Program System
Debugging Tips for Program Systems– Make sure that the function stub assigns a value to each output parameter.
Identifier Scope and Watch Window Variables
Black-Box Versus White-Box Testing
17
Modular programming
A program using RTL function sqrt() and user defined function sqrti()
Task problem: Square Root using standard math function and approximation by user defined function
yn+1 = (x/yn + yn)/2.
y0 = 1.
18
Modular programming
#include <iostream>#include <cmath>using namespace std;double sqrti(double arg, double prec);void main(){ double x, eps, res1, res2;
cout << "\nEnter values"; cin >> x >> eps;while ( !cin.eof() ){ if ( cin.fail() ) { cout << "\n Error input. Try again:"; exit(1); }
res1 = sqrt(x); res2 = sqrti(x, eps);cout << "\n square root of "<<x<<" is = "<<res1<<“ "<<res2;cout << "\n\nEnter new values or CTRL/Z to quit";cin >> x >> eps;
}}
19
Modular programming
double sqrti(double arg, double prec){
double yn, yn1;yn = 1;yn1 = (arg/yn + yn)/2.;while (fabs(yn-yn1) > prec ){
yn = yn1;yn1 = (arg/yn + yn)/2.;
}return yn1;
}
20
A program with multiple functions
Problem: to be able to perform computations with common fractions and get results that are common fractions in reduced form. We need a program to add, subtract, multiply and divide pairs of common fractions.
(see details in Hanly J.R., Koffman E.B., Problem Solving & Program Design in C, Addison-Wesley Publ. Comp., 3rd. ed., Chapter 6, pp 302-309)
21
A program with multiple functions
Analysis: Because the result is to be in reduced form, we’ll need to include a
fraction reducing function,
as well as functions
to add,
to subtract,
to multiply and
to divide common fractions.
22
A program with multiple functions
Data requirements:int n1, d1;// numerator/denominator for first fractionint n2, d2;// same for the second fractionchar op; // character for arithmetic operatorchar again; // ‘y’ or ‘n’ - user’s desire to continue Problem output;int nans; // numerator of answerint dans; // denominator of answer
23
A program with multiple functions
Design: We develop an algorithm through step wise refinement i.e. we look for instances in which a definition of a new function would simplify the design
Initial algorithm:1. Repeat as long as user wants to continue2. Get a fraction problem3. Compute the result4. Display problem and result5. Check if user wants to continue
24
A program with multiple functions
Problem 2: Get a fraction problem; split into three sub problems:
2.1 Get first fraction
2.2 Get operator
2.3 Get second fraction
25
A program with multiple functions
Problem 3: Compute the result; split into two sub problems:
3.1 Select a task based on operator‘+’ 3.1.1 Add the fractions‘–‘ 3.1.2 Add the first fraction and the negation of the second‘*’ 3.1.3 Multiply the fractions‘/’ 3.1.4 Multiply the first fraction and the reciprocal of the second
3.2 Put the result in reduced form3.2.1 Find gcd (greatest common divisor) of numerator and denominator3.2.2 Divide the numerator and denominator by gcd
26
A program with multiple functions. Function prototypes
Program with multiple functions to perform arithmetic operations on common fractions
void scan_fraction(int *nump, int *denomp);void addfraction(int n1, int d1, int n2, int d2,
int *nasp, int *dansp);void multiplyfraction(int n1, int d1, int n2, int d2,
int *nasp, int *dansp);int findgcd(int number1, int number2);void reducefraction(int *nump, int *denomp);void printfraction(int num, int denom);(details in H&K, Problem Solving & Program Design in C, Chap 6,
pp 300-315)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 5: Repetition and Loop Statements
Problem Solving,
Abstraction, and Design using C++ 5e
by Frank L. Friedman and Elliot B. Koffman
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28
5.9 Debugging and Testing Programs
• Modern Integrated Development Environments (IDEs) include features to help you debug a program while it is executing.
• If you cannot use a debugger, insert extra diagnostic output statements to display intermediate results at critical points in your program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29
Debugging Without a Debugger
• Insert diagnostic output statements– at locations where you suspect things are going
wrong or other critical points in the code• beginning and end of functions
• after complex calculations
– to display intermediate results, e.g. variables affected by each major algorithm step
• Remove extra output statements when problem solved, or use // to “comment out”
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 6:Modular Programming
Problem Solving,
Abstraction, and Design using C++ 5e
by Frank L. Friedman and Elliot B. Koffman
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 31
6.5 Debugging and Testing a Program System
• Keep each function to a manageable size– errors less likely– easier to read– simplifies testing
• Two kinds of testing– Top-down– Bottom-up
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32
Top-Down Testing and Stubs
• Useful for large projects
• Stubs for all functions not finished (substitute for a specific function) - just a heading without any details other than some type of message
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33
Listing 6.12 Stub for function computeSum
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 34
Bottom-Up Testing and Drivers
• Unit testing tests each function individually
• Driver used by developer to test full functionality of their function– contains only sufficient declarations and
executable statements to test a specific function
• System integration tests combine functions for additional testing
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35
Listing 6.13 A driver to test computeSum
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 36
Debugging Tips for Program Systems
• Use comments to– document each function parameter and local
variable– describe the function’s purpose
• Create a trace of execution by outputting the function name as the function is entered
• Trace (display) the values of all input and inout parameters upon function entry
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37
Debugging Tips for Program Systems
• Trace the values of all function outputs after returning from function. Verify. Make sure all inout and output parameters are declared as reference parameters (&).
• Make sure function stub assigns a value to each output parameter.
• Make sure function driver assigns a value to each input parameter.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 38
Identifier Scope and Watch Window Variables
• A debugger can help trace values passed into and out of a function. The values are displayed in a Watch window based on each identifier’s scope.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39
Black-Box Testing
• Assumes the program tester has no information about the code inside the function or system.
• Tester’s job is to – verify that the function or system meets specifications.
– for each function, ensure that post conditions are satisfied whenever its preconditions are met
– check for function crashing due to invalid input values
– check boundaries of system
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40
White-Box Testing
• Tester has full knowledge of function code
• Must ensure each section of code has been thoroughly tested.– Check all possible paths– Determine that for each test correct path is
taken– For loops, make sure correct number of
iterations– Check boundary values
41
Thank You
For
Your Attention!