Lecture 17: Modular Programming (cont) Debugging and Debuggers.

41
Lecture 17: Modular Programming (cont) Debugging and Debuggers

Transcript of Lecture 17: Modular Programming (cont) Debugging and Debuggers.

Page 1: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

Lecture 17:Modular Programming (cont)

Debugging and Debuggers

Page 2: 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

Page 3: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 4: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 5: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 6: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 7: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

7

MS Visual C++ Debugger

Page 8: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

8

MS Visual C++ Debugger

Page 9: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

9

MS Visual C++ Debugger

Page 10: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

10

MS Visual C++ Debugger

Page 11: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

11

MS Visual C++ Debugger

Page 12: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

12

MS Visual C++ Debugger

Page 13: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

13

MS Visual C++ Debugger

Page 14: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 15: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 16: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 17: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 18: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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;

}}

Page 19: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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;

}

Page 20: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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)

Page 21: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 22: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 23: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 24: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 25: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 26: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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)

Page 27: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 28: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 29: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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”

Page 30: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 31: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 32: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 33: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33

Listing 6.12 Stub for function computeSum

Page 34: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 35: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35

Listing 6.13 A driver to test computeSum

Page 36: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 37: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 38: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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.

Page 39: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 40: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

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

Page 41: Lecture 17: Modular Programming (cont) Debugging and Debuggers.

41

Thank You

For

Your Attention!