DOC PREVIEW
TRINITY CSCI 1320 - Repetition and Recursion

This preview shows page 1-2 out of 7 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Repetition and Recursion 9 26 2006 1 Opening 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 2 Recursion 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 3 static 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 4 The 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 5 File 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 6 Minute 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 n1 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 Thursday 7


View Full Document

TRINITY CSCI 1320 - Repetition and Recursion

Documents in this Course
Functions

Functions

10 pages

Functions

Functions

10 pages

Graphics

Graphics

10 pages

Graphics

Graphics

11 pages

Loops

Loops

4 pages

Loops

Loops

3 pages

Strings

Strings

9 pages

Functions

Functions

10 pages

Loops

Loops

11 pages

Graphics

Graphics

11 pages

Graphics

Graphics

12 pages

Sorting

Sorting

11 pages

Sorting

Sorting

10 pages

Arrays

Arrays

10 pages

Loops

Loops

18 pages

Load more
Download Repetition and Recursion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Repetition and Recursion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Repetition and Recursion and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?