10/23/11
1
Midterm Exam Review
“SuperFastMultipleLecturesinOne”
EECS 280
Programming and Introductory Data Structures
1
Exam Details
Exam locations
See CTools announcement
Exam time
Wed 26 Oct, 78:30pm (7:10p start)
Yes, the exam is only 80 minutes…
No notes – no book – no electronics – only a writing tool
2
Exam Format
Expected to be 5 Questions – 20 points each
80 minutes in length (this is always the case in EECS 280)
Topics you should definitely study:
Correctness, testing ,and exception handling
Recursion and Tail Recursion
Pointers, CStrings, 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
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
10/23/11
2
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 step’
6.
}
7.
}
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
(approximately).
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(n1,result*n);
}
}
int factorial(int num)
// REQUIRES: n >= 0
// EFFECTS: returns num!
{
return fact_helper(num, 1);
}
Tail Recursion
A Stack’s Best Friend
10
Outline
11
Procedural Abstraction
Recursion
Testing
Function Pointers
Arrays
Pointers
Structs and Enums
Templates
Exceptions
Testing
Endtoend vs. incremental testing
Errors made early in the programming process tend to be
pervasive and are hard to fix if you discover them late.
This is
nearly guaranteed to increase your workload.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '08
 NOBLE
 Lecturer, procedural abstraction, Introductory Data Structures, Function pointers

Click to edit the document details