summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorloshprung <lshprung@scu.edu>2020-02-14 11:33:49 -0800
committerloshprung <lshprung@scu.edu>2020-02-14 11:33:49 -0800
commitc25938a3064b1cc73af26ea8db297a55f26949bf (patch)
tree09d6be045c027429a5173ff55567a04ceec41282
parent76822cff123f3ea542449340f4b3ada51873203e (diff)
Post-class 02/14
-rw-r--r--02-12.md4
-rw-r--r--02-14.md120
2 files changed, 124 insertions, 0 deletions
diff --git a/02-12.md b/02-12.md
index ce40fa9..6e5dd1f 100644
--- a/02-12.md
+++ b/02-12.md
@@ -154,3 +154,7 @@ void compare(FILE *fp1, FILE *fp2){
return;
}
```
+
+---
+
+[-> Notes 02/14](02-14.md)
diff --git a/02-14.md b/02-14.md
new file mode 100644
index 0000000..fd184de
--- /dev/null
+++ b/02-14.md
@@ -0,0 +1,120 @@
+[\<- Notes 02/12](02-12.md)
+
+---
+
+# Arguments for main()
+
+To add arguments to main, do something like this:
+
+```
+int main(int argc, char *argv[])
+```
+
+- `int argc` is the number of arguments
+ - the name (`a.out`) is an argument, so there will always be at least one argument
+- `char *argv[]` is an array of strings, where each is an argument for the program
+- example, if you want two arguments, check that argc is the correct value: `argc == 3` (since you need one for the execution)
+
+# Recursion (not on Midterm #2)
+
+- A recursive function calls itself or is part of a cycle in the sequence of function calls
+ - Allows for the possibility for a function call to be repeated with different parameters
+
+- Alternative to iteration
+ - less efficient
+ - more simple
+
+- You will never use a loop ina recursive function
+ - recursion is meant to substitute the use of loops
+
+- Problems that lend themselves to a Recursive Solution:
+ - simple cases with a a straightforward solution
+ - cases can be redefined to be simplified
+ - redefinition process can reduce complex problems to simple cases (relatively easy to solve)
+
+General form:
+
+```
+if this is a **simple case**
+ solve it
+else
+ **redefine** the problem using recursion
+```
+
+Example: we want to multiply 6 by 3, but we only know how to add, and that x \* 1 = x
+
+```
+int multiply(int m, int n){
+ int ans;
+
+ if(n == 1) ans = m;
+ else ans = m + multiply(m, n-1);
+ return(ans);
+}
+```
+
+- Recursion can be useful for processing varying-length lists
+ - Strings
+ - Linked-lists
+ - Etc.
+
+Example: write a function to count the number of times a particular character appears in a string
+
+```
+int count(char ch, char *str){
+
+ if(*str == '\0') return 0;
+ if(ch == *str) return(1 + count(ch, str+1));
+ else return(count(ch, str+1));
+
+}
+```
+
+## Traversing Recursive Functions
+
+- *Hand Tracing* can be used to better understand how recursion works
+ - going through function calls step-by-step
+
+- Parameters and Local Variable Stacks
+ - Local function values are kept in a *stack*
+ - A *stack* is a data structure
+ - last data item in is first data item out
+ - *system stack* is an area in memory where parameters and local variables are
+ - allocated when a function is called
+ - deallocated (freed) when the function returns
+ - When a program is executing:
+ - Calling a function "pushes" its local values onto the top of the stack
+ - Returning from a function "pops" its local values from the top of the stack
+
+## Math Function and Recursion
+
+- Many mathematical functions are defined recursively
+
+- Example: Factorial
+ - 0! = 1
+ - n! = n \* (n-1)! for n>0
+- As a function:
+
+```
+int factorial(int n){
+ int ans;
+
+ if(n == 0) ans = 1;
+ else ans = n * factorial(n-1);
+
+ return(ans);
+}
+```
+
+- Example: The Fibonacci Sequence ([Spiral Out](https://www.youtube.com/watch?v=Y7JG63IuaWs))
+
+```
+int fibonacci(int n){
+ int ans;
+
+ if(n == 0 || n == 1) ans = n;
+ else ans = fibonacci(n-1) + fibonacci(n-2);
+
+ return ans;
+}
+```