CS 211:
Introduction to Computer Programming II
| Instructor: Brian M. Dennis | |
| Teaching Assistants: | |
| Tom Lechner, Bin Lin | |
| http://www.cs.northwestern.edu/~bmd/cs211/ | |
| int fib(int n) { | |
| if (n < 0) return 0; | |
| if (n == 0) return 0; | |
| if (n == 1) return 1; | |
| return fib(n-1) + fib(n-2); | |
| } |
| int fib(int n) { | |
| if ((n < 0) || (n == 0)) return 0; | |
| if (n == 1) return 1; | |
| return fib(n-1) + fib(n-2); | |
| } |
| // (0 <= x) && (x < 100) | |
| #include <cmath> | |
| double safe_invert(double val) { | |
| double z = 0.0; | |
| bool foo = false; | |
| foo = ((0.0 != val) && (z = 1.0 / val)); | |
| return z; | |
| } |
| // ! is the unary logical negation operator | |
| !((0 <= x) && (x <= 100)) | |
| è | |
| (!(0 <= x)) || (!(x <= 100)) | |
| è | |
| (x < 0) || (x > 100) |
| int fib(int n) { | |
| if (n < 0) return 0; | |
| if (n == 0) return 0; | |
| if (n == 1) return 1; | |
| return fib(n-1) + fib(n-2); | |
| } |
| int fib(int n) { | |
| if ((n < 0) || (n == 0)) return 0; | |
| if (n == 1) | |
| return 1; | |
| else | |
| n = n – 1; | |
| return fib(n-1) + fib(n-2); | |
| } |
| int fib(int n) { | |
| if (n < 0) { | |
| return 0; | |
| } else if (n == 0) { | |
| return 0; | |
| } else if (n == 1) { | |
| return 1; | |
| } else { | |
| n = n – 1; | |
| return fib(n-1) + fib(n-2); | |
| } | |
| } |
| int fib(int n) { | |
| int res = 0; | |
| switch(n) { | |
| case "blah": | |
| res = 0; | |
| case 1.0: | |
| res = 1; | |
| break; | |
| case res: | |
| default: | |
| res = fib(n-1) + fib(n-2); | |
| } | |
| return res; | |
| } |
| int fib_iter_helper(int a, int b, int count); | |
| int fib_iter(int n) { | |
| return fib_iter_helper(1, 0, n); | |
| } | |
| int fib_iter_helper(int a, int b, int count) { | |
| if (count == 0) return b; | |
| return fib_iter_helper(a + b, a, count - 1); | |
| } | |
| int fib_pure_iter(int n) { | |
| int a = 1; | |
| int b = 0; | |
| int old_a; | |
| do { | |
| old_a = a; | |
| a = a + b; | |
| b = old_a; | |
| n = n - 1; | |
| } while (0 < n); | |
| return b; | |
| } |
| int fib_pure_iter(int n) { | |
| int a = 1; | |
| int b = 0; | |
| int old_a = 1; | |
| while (0 < n) { | |
| old_a = a; | |
| a = a + b; | |
| b = old_a; | |
| n = n - 1; | |
| } | |
| return b; | |
| } |
| int fib_pure_iter(int n) { | |
| int a = 1; | |
| int b = 0; | |
| int old_a = 1; | |
| for (int i = 0; i < n; i = i + 1) | |
| { | |
| old_a = a; | |
| a = a + b; | |
| b = old_a; | |
| } | |
| return b; | |
| } | |
| int fib_pure_iter(int n) { | |
| int a, b, old_a; | |
| for (a = 1, b = 0, old_a = 1; | |
| 0 < n; | |
| n = n - 1 | |
| ) | |
| { | |
| old_a = a; | |
| a = a + b; | |
| b = old_a; | |
| }; | |
| return b; | |
| } | |
| void swap(int x, int y) { | |
| int old_x = x; | |
| x = y; | |
| y = old_x; | |
| } | |
| int main() { | |
| int x = 10, y = 20; | |
| swap(x, y); | |
| cout << "x: " << x; | |
| cout << " y: " << y; | |
| cout << "\n"; | |
| return 1; | |
| } |
| void swap(int& x, int &y) { | |
| int old_x = x; | |
| x = y; | |
| y = old_x; | |
| } | |
| int main() { | |
| int foo = 10, bar = 20; | |
| swap(x, y); | |
| cout << "x: " << x; | |
| cout << " y: " << y; | |
| cout << "\n"; | |
| return 1; | |
| } | |
| Takeaways | ||
| Logical operators | ||
| Conditional/selection | ||
| Iteration constructs | ||
| Call by reference | ||
| Reading | ||
| 2.1 – 2.7, | ||
| 2.13 – 2.20 | ||
| Get HW2 turnin instructions | ||