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

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Problem Solving and Program Design Programming. COMP104 Lecture 3 / Slide 2 Problem Solving Process l Define and analyze the problem. l Develop a solution.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Finding and Debugging Errors
Computer Programming 1 Problem Solving and Software Engineering.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
COS120 Software Development Using C++ AUBG Fall semester 2010
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Program Planning and Design Important stages before actual program is written.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
The Hashemite University Computer Engineering Department
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Introduction to Computer Programming
Lesson #6 Modular Programming and Functions.
Completing the Problem-Solving Process
Chapter 6: Modular Programming
Lesson #6 Modular Programming and Functions.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lesson #6 Modular Programming and Functions.
Designing and Debugging Batch and Interactive COBOL Programs
Lesson #6 Modular Programming and Functions.
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Lecture 17: Modular Programming (cont) Debugging and Debuggers

2 Lecture Contents: t Debugging and errors t Classification of errors: –Syntax errors; –Run-time errors; –Undetected errors; –Logic errors. t A program with multiple functions

3 Debugging and Debuggers t Debugging – removing errors from a program t Debugger – an utility program that helps the developer to register, detect, localize and repair errors. t IDE support built-in debugger: –Trace mode of execution –Watch variable values during execution, etc.

4 Errors classification t Syntax errors – detected at compile time: –missing semicolon, –undeclared identifier, … t Run time errors – detected at run time: –a=b/c; – division by zero error t Undetected errors: –mixture of character and numeric data at input, –uninitialized variable int sum; sum = sum + 30; t 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

9

10 MS Visual C++ Debugger

11 MS Visual C++ Debugger

12 MS Visual C++ Debugger

13 MS Visual C++ Debugger

Debugging and Testing a Program System t 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 t 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 t 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 t Debugging Tips for Program Systems –Make sure that the function stub assigns a value to each output parameter. t Identifier Scope and Watch Window Variables t 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 y n+1 = (x/y n + y n )/2. y 0 = 1.

18 Modular programming #include using namespace std; double sqrti(double arg, double prec); void main() {double x, eps, res1, res2; cout > 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., 3 rd. ed., Chapter 6, pp )

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 fraction int n2, d2;// same for the second fraction char op;// character for arithmetic operator char again;// ‘y’ or ‘n’ - user’s desire to continue Problem output; int nans;// numerator of answer int 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 continue 2. Get a fraction problem 3. Compute the result 4. Display problem and result 5. 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 ‘+’ Add the fractions ‘–‘ Add the first fraction and the negation of the second ‘*’ Multiply the fractions ‘/’ Multiply the first fraction and the reciprocal of the second 3.2 Put the result in reduced form Find gcd (greatest common divisor) of numerator and denominator 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 )

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 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 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!