This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Midterm Exam Review “SuperFastMultiple LecturesinOne” EECS 280 Programming and Introductory Data Structures 1 Exam Details Exam locations See the CTools announcement No notes – no book – no electronics – only a writing tool 2 Exam Format Expected to be 5 Questions – 20 points each Topics you should definitely study: Correctness, testing ,and exception handling Recursion and Tail Recursion Pointers, arrays, and functions pointers Lists and Trees (project 2) Enum and Structs (project 3) 3 Outline 4 Procedural Abstraction Recursion Testing Function Pointers Arrays Pointers Structs and Enums Templates Exceptions Procedural Abstraction Specification Comments You can never change the abstraction (which includes function headers), but you can change your implementation . We use specifications to describe our abstractions. There are three clauses to the specification: REQUIRES: the preconditions that must hold, if any. MODIFIES: how inputs are modified, if any. EFFECTS: what the procedure computes given legal inputs. 5 Outline 6 Procedural Abstraction Recursion Testing Function Pointers Arrays Pointers Structs and Enums Templates Exceptions Recursion A convenient place for using stacks “Recursive” just means “refers to itself”. So, a function is recursive if it calls itself. Likewise, a problem is recursive if: 1. There is (at least) one “trivial” base or “stopping” case. 2. All other cases can be solved by first solving one (or more) smaller cases, and then combining those solutions with a simple step. 7 Recursion Example 1 (n == 0) n * (n1)! (n > 0) n! = int factorial (int n) // REQUIRES: n >= 0 // EFFECTS: computes n! 1. { 2. if (n == 0) { 3. return 1; // ‘base case’ 4. } else { 5. return n*factorial(n1); // ‘recursive 8 Recursion Another kind of factorial int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n1, result * n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } Rewrite the recursive version to use the same amount of space as is required by the iterative version (approximatel y). 9 With tail recursion, there is no pending computation at each recursive step, so we can reuse the activation record rather than create a new one. int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n 1,result*n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num!...
View
Full
Document
This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.
 Winter '08
 NOBLE
 Data Structures

Click to edit the document details