Lec16Review1InClass

# Lec16Review1InClass - Recursive examples: Three parts: 1)...

This preview shows pages 1–2. Sign up to view the full content.

Recursive examples: Three parts: 1) base case (when and what to do/return) 2) smaller case (how to chop up the problem) 3) general case (how to tie the answer to the smaller problem to the larger problem) Sometimes, the answer to the larger problem IS the answer to the smaller problem – tail recursion Print out all of the nodes in a linked list in opposite order 1) one node, print that node 2) print out from the second node in the list to the last node in opposite order 3) first do part2, then print out the head node Count the number of occurrences of the letter ‘r’ in a string 1) if the character is ‘\0’, return 0 2) Count the number of occurrences of the letter ‘r’ in the string, not including the first character 3) If first character is ‘r’, return part2+1, else return part2 A positive integer is said to be “perfect” if the sum of its factors (excluding the integer itself) is that integer. For example: 6: 3, 2, 1 1 + 2 + 3 = 6 -> 6 is perfect. 28: 1, 2, 4, 7, 14 Sum those, you get 28. 28 is also perfect.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/27/2011 for the course CMPSC 24 taught by Professor Agrawal during the Fall '09 term at UCSB.

### Page1 / 2

Lec16Review1InClass - Recursive examples: Three parts: 1)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online