{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Recursion - UAH CPE 212 Fundamentals of Software...

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

UAH CPE 212 Fundamentals of Software Engineering Agenda Class 15 Recursion Recursion Key Concepts

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

View Full Document
UAH CPE 212 Today Last Time Priority Queues This Time Recursion
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; if (n & 1) return Power(x, n-1) * x; else return power (x*x, n/2); }

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

View Full Document
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 } }
UAH CPE 212 Recursion 3 ostream& operator << (ostream& out, const unsigned int value) { 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

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

View Full Document
UAH CPE 212 Recursion Examples Fractals Mazes
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern