From c25938a3064b1cc73af26ea8db297a55f26949bf Mon Sep 17 00:00:00 2001 From: loshprung Date: Fri, 14 Feb 2020 11:33:49 -0800 Subject: Post-class 02/14 --- 02-12.md | 4 +++ 02-14.md | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 02-14.md 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; +} +``` -- cgit