1Repetition and Recursion9-26-20062Opening Discussion■Do you have any questions about the quiz?■Writing a loop to sum numbers.■What are the primary structures of any loop?■What are the different loops in the the C language?■Do you have any questions about the reading?■Do you have any questions about the assignment?3Recursion■What is a recursive function?■What things can you do with recursive functions?■What happens to the variables in a function when you call it?■How can we imitate simple loops with recursion?■Let's look at factorial as a recursive function.4static Local Variables■There is a keyword in C that we haven't talked about called static.■When you use static to modify a local variable it tells C that the value of that variable should be remembered across calls to the function.■It is like having a global variable but with local scope. In fact, it likely goes into memory next to global variables but you just can't access it anywhere else.■This isn't used all the frequently in C, but it might be useful for at least one of the assignment options.5The Power of Recursion■If all we could do with recursion was the same things we can do with loops, it wouldn't be of interest. The truth is that recursion is FAR more powerful than simple loops because of the memory provided by the call stack.■A simple example of this is a program that reads in a set of numbers and prints them in reverse order.■More complex examples involve functions that call themselves more than once. Those functions are very hard to convert over to loops.■Our example code last time did exactly this. Let's go look at it in detail.6File I/O■So far the only input and output we have done are to standard I/O. Programs are often far more useful if they can do I/O with a user and with files at the same time. We can't do that yet.■To do this we need to introduce a few new functions from the stdio library.■Basically, we need to be able to open and close files, then use modified versions of printf and scanf (fprintf and fscanf).7Minute Essay■The Fibonacci sequence is defined to begin with the numbers 1 and 1 and every number after that is the sum of the previous two. That is f(n)=f(n-1)+f(n-2) for n>2 else f(n)=1. Write this as a C function.■Can you think of any reason this might not be a good way to calculate the Fibonacci sequence?■Assignment #3 is due on
View Full Document