Explore BrainMass

Examples of recursion in C++

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

A. What is direct recursion?

b. What is tail recursion?

c. Suppose hat intArray is an array of integers, and length specifies the number of elements in intArray. Also suppose that low and high are two integers such that 0 <= low < length. 0 <= high < length, and low < high. That is, low and high are two indices in intArray. Write a recursive definition that reverses the elements in intArray between low and high.

d. please see the attachment.

© BrainMass Inc. brainmass.com March 21, 2019, 5:05 pm ad1c9bdddf


Solution Preview

Direct recursion is when recursion is obtained through a function directly calling itself. This in in constrast to indirect recursion which happens when function A calls function B which then calls function A again.

Tail recursion is a special form of recursion in which the recursive call is at the end of the function body. The following is an example of tail recursion:

function count(int x) {
if (x == 0) {
print x;
} else {

Notice that the call back into the function itself is at the of the body of the ...

Solution Summary

This solution provides a couple straight-forward examples of recursion in C++. It also explains how the recursion is working.