Close
Register
Close Window

CSci 2101 Data Structures

Chapter 11 Recursion

| About   «  11.1. Introduction to Recursion   ::   Contents   ::   11.3. Code Completion Practice Exercises  »

11.2. Writing a recursive function

11.2.1. Writing a recursive function

Solving a “big” problem recursively means to solve one or more smaller versions of the problem, and using those solutions of the smaller problems to solve the “big” problem. In particular, solving problems recursively means that smaller versions of the problem are solved in a similar way. For example, consider the problem of summing values of an array. What’s the difference between summing the first 50 elements in an array versus summing the first 100 elements? You would use the same technique. You can even use the solution to the smaller problem to help you solve the larger problem.

Here are the basic four steps that you need to write any recursive function.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Now le’t see some different ways that we could write Sum recursively.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

   «  11.1. Introduction to Recursion   ::   Contents   ::   11.3. Code Completion Practice Exercises  »

Close Window