{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_9_Recursion1

# COP3502_9_Recursion1 - Recursion Computer Science...

This preview shows pages 1–9. Sign up to view the full content.

Computer Science Department University of Central Florida Recursion COP 3502 – Computer Science I

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion page 2 Recursion What is Recursion? Powerful, problem-solving strategy “yeah, that tells us a whole lot” </sacrasm_off> In plain English: Recursion: the process a procedure goes through, when one of the steps of the procedure involves rerunning the entire procedure Example: say that some procedure has 4 steps The 3 rd step instructs you to run the entire procedure again Each time you get to the third step, you have to start anew This goes on, potentially, infinitely And this is an example of Recursion
Recursion page 3 Recursion: Ex of Thinking Recursively Strategy for processing nested dolls: INITIATE FUNCTION “Open All Dolls” if there is only one doll you’re done! Play with the doll. else open the outer doll Process the inner nest in the same way

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion page 4 Recursion What is Recursion? From the programming perspective: A recursive function is one that contains a call to its own self Example: we know that we are allowed to call function B from within function A Also, you are allowed to call function A from within function A! This is recursion Note: This could go on for infinity as function A keeps calling function A So we must have a way to exit the function!
Recursion page 5 Recursion What is Recursion? From the programming perspective: Recursion solves large problems by reducing them to smaller problems of the same form Again, recursion is a function that invokes itself Basically splits a problem into one or more SIMPLER versions of itself And we must have a way of stopping the recursion So the function must have some sort of calls or conditional statements that can actually terminate the function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion page 6 Recursion Example w/o terminate #include <stdio.h> void print(); int main() { print(); system("PAUSE"); return 1; } void print() { printf(“Example of recursion WITHOUT a stopping case.\n"); print(); } Example of recursion without a terminating condition. Just keeps going and going and…
Recursion page 7 Recursion Programming example: Let us write a program that counts down from 10 and then prints BLAST OFF! How would we do this iteratively? This program prints: 10! 9! 8! 7! 6! 5! 4! 3! 2! 1! BLAST OFF! #include <stdio.h> int main(void) { int i; for (i = 10; i > 0; --i) printf(“%d! ”, i); printf(“\nBLAST OFF!\n”); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion page 8 Recursion How do we do this recursively? We need a function that we will call And this function will then call itself until the stopping case Once again, this program prints: 10! 9! 8! 7! 6! 5! 4! 3! 2! 1! BLAST OFF!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

COP3502_9_Recursion1 - Recursion Computer Science...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online