Docsity

Recursion in C++, Summaries of Computer science

Partial preview of the text

Download Recursion in C++ and more Summaries Computer science in PDF only on Docsity!

Recursion in C++

CS 16: Solving Problems with Computers I Lecture # Ziad Matni Dept. of Computer Science, UCSB

Lecture Outline

  • Linked Lists: solution to homework #
  • Recursion in C++

Other Functions We Might Create for LLs…

  • Insert node at the head
  • Print out all the values in the LL
  • Search the LL for a target
  • Insert node at the end of LL
  • Insert node anywhere in the LL
  • Delete a node according to some target value criteria
  • Sort an LL according to some target value criteria etc…

Recursive Functions

  • Recursive: (adj.) Repeating unto itself
  • A recursive function contains a call to itself
  • When breaking a task into subtasks, it may be that the subtask is a smaller example of the same task

Example: The Factorial Function

Recall: x! = 1 * 2 * 3 … * x You could code this out as either:

  • A loop: (for k=1; k < x; k++) { factorial *= k; }
  • Or a recursion/repetition: factorial(x) = x * factorial(x-1) = x * (x-1) * factorial (x-2) = etc… until you get to factorial(1) (then what?!?)

The Base Case

  • If we assume that we start the sequence at n = 1… (an arbitrary value) … then we could devise an algorithm for a(n) like this:
  1. If n = 1, then return 5 to a(n)
  2. Otherwise, return a(n-1) + 5
  • I’ll need to know what that base case is, otherwise I risk not ending my recursion (or not making sense of it)

an = an-1 + 5

The BASE case The RECURSION (i.e. the function calling itself)

Case Study : Vertical Numbers

  • Problem Definition: Write a recursive function that takes an integer number and prints it out one digit at a time vertically :

void write_vertical( int n ); //Precondition: n >= 0 //Postcondition: n is written to the screen vertically // with each digit on a separate line

Case Study : Vertical Numbers

Algorithm design

  • Simplest case (what do we call that again???) If n is 1 digit long, just write the number
  • More typical case :
    1. Output all but the last digit vertically (recursion!)
    2. Write the last (least significant) digit (base case!)
  • Step 1 is a smaller version of the original task - The recursive case
  • Step 2 is the simplest case - The base case

Case Study : Vertical Numbers

The write_vertical algorithm:

void write_vertical( int n ) { if (n < 10) cout << n << endl; // n < 10 means n is only one digit else // n is two or more digits long { write_vertical ( n-with-the-least-significant-digit-removed ); cout << the least-significant digit of n << endl; } }

Case Study : Vertical Numbers

The write_vertical function in C++

void write_vertical( int n ) { if (n < 10) cout << n << endl; // n < 10 means n is only one digit else // n is two or more digits long { write_vertical (n / 10); cout << (n % 10) << endl; } }

Example Run

write_vertical(543)

write_vertical(54)

write_vertical(5) (^) cout << 5 << endl;

void write_vertical( int n ){ if (n < 10) cout << n << endl;else { write_vertical(n / 10); } cout << n % 10 << endl; }

cout << 4 << endl;

cout << 3 << endl;

stdout: 5 4 3

①

②

③

④

⑤

“Infinite” Recursion

  • In practice , the computer will often run out of resources (i.e. memory usually) and the program will terminate abnormally - This can happen even in non-infinite recursion situations! (can you think of a case where this could happen?)
  • So… remember that computers are machines, not Math Gods and design your (recursive) functions with that in mind!

Stacks for Recursion

  • Computers use a memory structure called a stack to keep track of recursion
  • Stack : a computer memory structure analogous to a stack of paper
    • Start at zero: no papers, just knowledge of where to start (via a “stack pointer”)
    • To place data on the stack: write it on a piece of paper and place it on top of the stack
    • To insert more information on the stack: use a new sheet of paper, write the information, and place it on the top of the stack
    • Keep going… until you don’t…
    • To retrieve information: you can only take the top sheet of paper
      • Then throw it away when it you’re done “reading” it
    • If you want access to any paper farther down, go thru the stack to get to it