• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • RECURSION

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 5

RECURSION - all with Video Answers

Educators


Chapter Questions

01:22

Problem 1

The set of natural numbers $\mathrm{N}$ defined at the beginning of this chapter includes the numbers $10,11, \ldots, 20,21, \ldots$ and also the numbers $00,000,01,001, \ldots$ Modify this definition to allow only numbers with no leading zeros.

Ian Maurer
Ian Maurer
Numerade Educator

Problem 2

Write a recursive function that calculates and returns the length of a linked list.

Check back soon!
05:07

Problem 3

What is the output for the following version of reverse ():
void reverse () {
int ch;
cin.get (ch);
if $\left(\mathrm{ch} !=' \backslash \mathrm{n}^{\prime}\right)$
reverse();
cout, put (ch);
}

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator

Problem 4

What is the output of the same function if ch is declared as
static char ch;

Check back soon!
02:02

Problem 5

An early application of recursion can be found in the sixteenth century in John Napier's method of finding logarithms. The method was as follows:
start with two numbers $\mathrm{n}, \mathrm{m}$ and their logarithms logn, logm if they are known;
while not done
for a geometric mean of two earlier numbers find a logarithn which is an arithmetic mean of two earlier logarithms, that is, $\operatorname{logk}=$ $(\operatorname{logn}+\operatorname{logm}) / 2$ for $\mathrm{k}=\sqrt{\mathrm{nm}}$;
proceed recursively for pairs $(\mathrm{n}, \backslash \overline{\mathrm{nm}})$ and $(\backslash \overline{\mathrm{nm}}, \mathrm{m})$;
For example, the 10-based logarithms of 100 and 1000 are numbers 2 and 3 , the geometric mean of 100 and 1000 is 316.23 , and the arithmetic mean of their logarithms, 2 and 3 , is 2.5 . Thus, the logarithm of 316,23 equals 2.5 . The process can be continued: The geometric mean of 100 and 316.23 is 177.83 whose logarithm is equal to $(2+2.5) / 2=2.25$.
a. Write a recursive function logarithm() that outputs logarithms until the difference between adjacent logarithms is smaller than a certain small number.
b. Modify this function so that a new function logarithmof () finds a logarithm of a specific number $x$ between 100 and 1000 . Stop processing if you reach a number $y$ such that $y-\mathrm{x}<\epsilon$ for some $\epsilon$.
c. Add a function which calls logar ithmof () after determining between what powers of 10 a number $x$ falls so that it does not have to be a number between 100 and 1000.

Manik Pulyani
Manik Pulyani
Numerade Educator
03:15

Problem 6

The algorithms for both versions of the power function given in this chapter are rather simpleminded. Is it really necessary to make eight multiplications to compute $x^8 ?$ It can be observed that $x^8=\left(x^4\right)^2, x^4=\left(x^2\right)^2$, and $x^2=x \cdot x$, that is, only three multiplications are needed to find the value of $x^\kappa$. Using this observation, improve both algorithms for computing $x^{\prime \prime}$.

Lucas Gagne
Lucas Gagne
Numerade Educator
04:50

Problem 7

Execute by hand the functions tail() and nontail() for the parameter values of 0,2 , and 4 . Definitions of these functions are given in Section 5.4.

Ahmad Reda
Ahmad Reda
Numerade Educator
05:54

Problem 8

Write a function that recursively converts a string of numerals into an integer. For instance, convert ("1234 ") would return the integer 1234.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
00:56

Problem 9

Write a recursive function to compute the binomial coefficient according to the definition
$$
\left(\begin{array}{l}
n \\
k
\end{array}\right)= \begin{cases}1 & \text { if } k=0 \text { or } k=n \\
\left(\begin{array}{l}
n-1 \\
k-1
\end{array}\right)+\left(\begin{array}{c}
n-1 \\
k
\end{array}\right) & \text { otherwise }\end{cases}
$$

David Nguyen
David Nguyen
Numerade Educator
00:52

Problem 10

Write a recursive function to add the first $n$ terms of the series
$$
1+\frac{1}{2}-\frac{1}{3}+\frac{1}{4}-\frac{1}{5} \cdots
$$

Amy Jiang
Amy Jiang
Numerade Educator
02:52

Problem 11

Write a recursive function $\operatorname{GCD}(\mathrm{n}, \mathrm{m})$ that returns the greatest common divisor of two integers $\mathrm{n}$ and $\mathrm{m}$ according to the following definition:
$$
\operatorname{GCD}(n, m)= \begin{cases}m & \text { if } m \leq n \text { and } n \bmod m=0 \\ \operatorname{GCD}(m, n) & \text { if } n<m \\ \operatorname{GCD}(m, n \bmod m) & \text { otherwise }\end{cases}
$$

Lucas Gagne
Lucas Gagne
Numerade Educator
02:25

Problem 12

Give a recursive version of the following function:
void cubes(int $n$ ) {
for (int $i=1 ; i \ll=n ; 1++)$
cout $<1 * 1 * i \ll+$;
}

WM
William Mead
Numerade Educator

Problem 13

Check recursively if the following objects are palindromes:
a. a word
b. a sentence (ignoring blanks, lower- and uppercase differences, and punctuation marks so that "Madam, I'm Adam" is accepted as a palindrome)

Check back soon!
01:03

Problem 14

For a given character recursively, without using strchr () or strrchr(),
a. Check if it is in astring.
b. Count all of its occurrences in a string.
c. Remove all of its occurrences from a string.

Aman Gupta
Aman Gupta
Numerade Educator
01:03

Problem 15

Write equivalents of the last three functions for substrings (do not use strstr ()).

Aman Gupta
Aman Gupta
Numerade Educator
01:36

Problem 16

What changes have to be made in the program in Figure 5.6 to draw a line as in Figure 5.21? Try it and experiment with other possibilities to generate other curves.

Abdel Osman
Abdel Osman
Numerade Educator
01:35

Problem 17

Create a tree of calls for $\sin (x)$ assuming that only $\frac{x}{18}$ (and smaller values) do not trigger other calls.

Carson Merrill
Carson Merrill
Numerade Educator

Problem 18

Write recursive and nonrecursive functions to print out a nonnegative integer in binary. The functions should not use bitwise operations.
figure 5.21 can't copy

Check back soon!
00:41

Problem 19

The nonrecursive version of the function for computing Fibonacci numbers uses information accumulated during computation, whereas the recursive version does not. However, it does not mean that no recursive implementation can be given which can collect the same information as the nonrecursive counterpart. In fact, such an implementation can be obtained directly from the nonrecursive version. What would it be? Consider using two functions instead of one; one would do all the work, the other would only invoke it with the proper parameters.

James Kiss
James Kiss
Numerade Educator
04:42

Problem 20

The function putQueen () does not recognize that certain configurations are symmetric. Adapt function putqueen () for a full $8 \times 8$ chessboard, write the function printBoard (), and run a program for solving the eight queens problem so that it does not print symmetric solutions.

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 21

Finish the trace of execution of putqueen () shown in Figure 5.18.

Check back soon!
00:12

Problem 22

Execute the following program by hand from the case study, using these two entries:
a. $v=x+y * w-z$
b. $v=x *(y-w)--z$
Indicate clearly which functions are called at which stage of parsing these sentences.

Vishal Parmar
Vishal Parmar
Numerade Educator
04:29

Problem 23

Extend our interpreter so that it can also process exponentiation, $\wedge$. Remember that exponentiation has precedence over all other operations so that $2-3^{\wedge} 4 * 5$ is the same as $2-\left(\left(3^{\wedge} 4\right)^* * 5\right)$. Notice also that exponentiation is a right-associative operator (unlike addition or multiplication); that is, $2^{\wedge} 3^{\wedge} 4$ is the same as $2^{\wedge}\left(3^{\wedge} 4\right)$ and not $(2^{\wedge} 3)^{\wedge} 4$.

Pammi Eswari
Pammi Eswari
Numerade Educator
01:46

Problem 24

In $\mathrm{C}++$, the division operator, /, returns an integer result when it is applied to two integers; for instance, 11/5 equals 2 . However, in our interpreter, the result is 2.2 . Modify this interpreter so that division works the same way as in $\mathrm{C}++$.

Narayan Hari
Narayan Hari
Numerade Educator

Problem 25

Our interpreter is unforgiving when a mistake is made by the user, since it finishes execution if a problem is detected. For example, when the name of a variable is mistyped when requesting its value, the program notifies the user and exits and destroys the list of identifiers. Modify the program so that it continues execution after finding an error.

Check back soon!

Problem 26

Write the shortest program you can that uses recursion.

Check back soon!