Lecture 4

# 9 2 if you keep deriving simpler and simpler problems

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (3) ; // writes 6 to the screen. 9 2 - If you keep deriving simpler and simpler problems of the “same kind”, you will eventually reach an “easy” problem. Let’ look at factorial again, to understand s this. factorial(n) = n * factorial(n - 1) --- if n > 0 factorial(n) = 1 --- if n = 0 11 10 1 - factorial(n) is decomposed into two problems: A multiplication and factorial(n-1) is a problem of the same kind as factorial(n), but factorial(n-1) is also simpler then factorial(n). 2 - If we keep reducing the argument of factorial( ) more and more, eventually, we will get factorial(0) which is “easy” to solve. 12 Example: Writing an array backwards Given is an array of characters, and we want to write its elements backwards to the screen, i.e., starting with the last character. Of course this can be done with a for (x = ArraySize-1...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online