COP3502_9_Recursion1 - Recursion Computer Science...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Recursion COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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!
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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…
Background image of page 6
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”); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
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!
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/21/2011.

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 Right Arrow Icon
Ask a homework question - tutors are online