ENERGY 211 CME 211 - PowerPoint PPT Presentation

About This Presentation
Title:

ENERGY 211 CME 211

Description:

A recursive function is a function that, directly or indirectly, calls itself ... IDEs, like MDS and Microsoft Visual Studio, typically include debuggers ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 11
Provided by: lamb8
Learn more at: https://web.stanford.edu
Category:
Tags: cme | energy | ides

less

Transcript and Presenter's Notes

Title: ENERGY 211 CME 211


1
ENERGY 211 / CME 211
  • Lecture 13
  • October 20, 2008

2
Recursion
  • A recursive function is a function that, directly
    or indirectly, calls itself
  • Each function call has its own copy of parameters
    and local variables
  • Internally, function calls are maintained using a
    stack
  • Make sure that recursion will end!

3
Example Factorial
  • int factorial(int n)
  • if (n0)
  • return 1
  • return nfactorial(n1)
  • This will compute n! for any nonnegative integer
    n
  • What is wrong with this function?

4
Applications of Recursion
  • The factorial example is a simple illustration of
    recursion, but it can easily be implemented
    non-recursively
  • Recursion useful whenever an algorithm solves a
    problem by applying itself to smaller instances
    of the problem
  • Often, algorithms are more rapidly designed
    recursively, which encourages rapid application
    development

5
Mergesort
  • Simplistic algorithms for sorting an array of n
    elements take O(n2) comparisons
  • In mergesort, the array is divided in half, and
    each half is sorted individually
  • The two sorted halves are then merged in O(n)
    comparisons, for O(n log2n) comparisons overall
  • Limiting case of recursion an array with one
    element is already sorted!

6
Fast Fourier Transform
  • The Fast Fourier Transform (FFT) computes the
    discrete Fourier transform of a vector of
    functions values
  • It recursively transforms odd-numbered and
    even-numbered points, and then merges the results
  • O(n2) work reduced to O(n log2n)
  • For efficiency, should use pointers that take
    larger and larger steps to access elements!

7
Debuggers
  • A debugger is a tool that performs step-by-step
    execution of a program, allowing the programmer
    to study its behavior and find bugs
  • IDEs, like MDS and Microsoft Visual Studio,
    typically include debuggers
  • A debugger runs your program, stopping execution
    at user-specified breakpoints to allow inspection

8
The MDS Debugger
  • Before debugging, use F9 to set breakpoints in
    your program
  • F5 begins execution within the debugger, or
    resumes after breakpoint
  • When stopped, F10 steps through next line
  • F11 steps into function call
  • Shift-F11 steps out of function
  • Hover over variables to see values

9
The gdb Debugger
  • gdb is the GNU debugger
  • Run from UNIX prompt with executable filename as
    argument
  • break command sets breakpoint, can specify
    function name or line number
  • run command begins execution
  • step command advances one line, steps into
    functions automatically
  • cont resumes execution
  • print shows value of given expression

10
Next Time
  • Testing strategies
  • Debugging strategies
  • Designing software with testing and debugging in
    mind!
Write a Comment
User Comments (0)
About PowerShow.com