Short Answers Section 9.1 Recursive Functions |
For example, suppose that you start with 250 bears. Then you could make
these moves:
Write a recursive function to meet this specification:
The string class from <
void pattern(unsigned int n)
// Precondition: n >
The goal of the game is to end up with EXACTLY 42 bears.
--Start with 250 bears.
--Since 250 is divisible by 5, you may return 42 of the bears,
leaving you with 208 bears.
--Since 208 is even, you may return half of the bears,
leaving you with 104 bears.
--Since 104 is even, you may return half of the bears,
leaving you with 52 bears.
--Since 52 is divisible by 4, you may multiply the last two digits
(resulting in 10) and return these 10 bears. This leaves you
with 42 bears.
--You have reached the goal!
bool bears(int n)
// Postcondition: A true return value means that it is possible to win
// the bear game by starting with n bears. A false return value means that
// it is not possible to win the bear game by starting with n bears.
// Examples:
// bear(250) is true (as shown above)
// bear(42) is true
// bear(84) is true
// bear(53) is false
// bear(41) is false
// Hint: To test whether n is even, use the expression ((n % 2) == 0).
void permute(string first, string second)
// Postcondition: The output consists of lines of Strings. Each String
// is made by writing some rearrangement of first followed by all
// of second.
// For example, if first is "CAT" and second is "MAN", then there will
// be six lines of output:
// CATMAN
// ACTMAN
// TCAMAN
// CTAMAN
// ATCMAN
// TACMAN
Hints: The stopping case occurs when the length of first is zero (in which case
the function prints second). Some string member functions that will help you
for a String s:
#include <
The function prints output to the ostream outs.
The output consists of
the string prefix followed by "section numbers" of the
form 1.1., 1.2., 1.3., and so on. The levels argument determines how may
levels the section numbers have. For example, if levels is 2, then the section
numbers have the form x.y. If levels is 3, then section numbers have the
form x.y.z. The digits permitted in each level are always '1' through '9'.
As an example, if prefix is the string "THERBLIG" and levels is 2, then the
function would print 81 strings. In this case, the function
starts by printing:
THERBLIG1.1.
THERBLIG1.2.
THERBLIG1.3.
and ends by printing:
THERBLIG9.7.
THERBLIG9.8.
THERBLIG9.9.
The stopping case occurs when levels reaches zero (in which case the
prefix is printed once by itself followed by nothing else).
s = (prefix + c) + '.';
This new string s can be passed as a parameter to recursive calls of the
function.
Short Answers
Section 9.2
Fractals
and Mazes
n=1: Output is: 1
n=2: Output is: 1 2 1
n=3: Output is: 1 2 1 3 1 2 1
n=4: Output is: 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
And so on. Note that the output for n always consists of the output
for n-1, followed by n itself, followed by a second copy of the output
for n-1.
bool dead_end()
// Postcondition: The return value is true if the direction directly
// in front is a dead end (i.e., a direction that cannot contain the
// tapestry).
// Library facilities used: useful.h (from Appendix 1).
{
return inquire("Are you facing a wall?")
||
inquire("Is your name written in front of you?");
}
Explain why the function dead_end sometimes asks 2 questions and
sometimes asks only 1.
Short Answers
Section 9.3
Reasoning About
Recursion
Multiple Choice Section 9.1 Recursive Functions |
void super_write_vertical(int number)
// Postcondition: The digits of the number have been written,
// stacked vertically. If number is negative, then a negative
// sign appears on top.
// Library facilities used: iostream.h, math.h
{
if (number <
What values of number are directly handled by the stopping case?
void super_write_vertical(int number)
// Postcondition: The digits of the number have been written,
// stacked vertically. If number is negative, then a negative
// sign appears on top.
// Library facilities used: iostream.h, math.h
{
if (number <
Which call will result in the most recursive calls?
void quiz(int i)
{
if (i >
How many asterisks are printed by the function call quiz(5)?
XXXXXXXXXX
XX XXXX This is row number 1
XX XX XXXX This is row number 2
XX o XXX This is row number 3
XXXXXXXXXX
Suppose that the first recursive call is always left; the second recursive
call is always right; the third recursive call is always up; the fourth
recursive call is always down.
How will the rows be completely filled?
void test_a(int n)
{
cout <
What is printed by the call test_a(4)?
void test_b(int n)
{
if (n>0)
test_b(n-2);
cout <
What is printed by the call test_b(4)?
Multiple Choice
Section 9.2
Fractals
and Mazes
Multiple Choice
Section 9.3
Reasoning about
Recursion
Data Structures and Other Objects Using C++
Thank you for visiting
http://www.cs.colorado.edu/~main/questions/chap09q.html
Copyright © 2000
Addison-Wesley Computer and Engineering Publishing Group