Recursion - UAH CPE 212 Fundamentals of Software...

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

View Full Document Right Arrow Icon
UAH CPE 212 Fundamentals of Software Engineering Agenda Class 15 Recursion Recursion Key Concepts
Background image of page 1

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Today Last Time Priority Queues This Time Recursion
Background image of page 2
UAH CPE 212 Recursion Procedure calls itself Recursion: Divide and Conquer Steps: Define Base Case Arrange calls to work to a solution Inductively assume all cases make progress Identify termination Do Not Forget the stack Has short memory x 0 = 1 x 1 = x x n = (x 2 ) n/2 even x n = x n-1 x odd float Power(float x, int n) { if (n == 0) return 1; if (n == 1) return x; return Power(x, n-1) * x; else return power (x*x, n/2); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Recursion 2 int main( ) { int i; cout << "Please type a non-negative number: "; cin >> i; cout << "The digits of that number are:" << endl; write_vertical(i); cout << "Please type a negative number: "; cin >> i; cout << "The digits of that number are:" << endl; super_write_vertical(i); cout << "Let's get vertical!" << endl; return EXIT_SUCCESS; } void write_vertical(unsigned int number) // Postcondition: The digits of number have been written, stacked vertically. // Library facilities used: iostream.h { if (number < 10) cout << number << endl; // Write the one digit else { write_vertical(number/10); // Write all but the last digit cout << number % 10 << endl; // Write the last digit } }
Background image of page 4
UAH CPE 212 Recursion 3 { if (value < 10 ) out << digitCharacter(value); else { out << (value / 10); out << digitCharacter(value % 10); } return out; } print 456 print 45, then print 6 print 4, then print 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Recursion Examples Fractals Mazes
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2008 for the course CPE 212 taught by Professor Skipper during the Spring '07 term at University of Alabama in Huntsville.

Page1 / 15

Recursion - UAH CPE 212 Fundamentals of Software...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online