Share
Explore BrainMass

# Examples of recursion in C++

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.

#### 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:

<pre>
function count(int x) {
if (x == 0) {
print x;
} else {
count(x-1);
}
}
</pre>

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.

\$2.19