Chapter 15 Flashcards
True or false:
The following is a valid recursive definition to determine the factorial of a non-negative integer.
0! = 1
1! = 1
n! = n * (n - 1)! if n > 0
True
True or false:
In a recursive function, the base case stops the recursion.
False
True or false:
With recursion, the base case must eventually be reduced to a general case.
False
True or false:
The following is an example of a recursive function.
void print(int x)
{
if (x > 0)
{
cout «_space;x «_space;” “ ;
print (x - 1);
}
}
True
True or false:
Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters
True
True or false:
Infinite recursions execute forever on a computer.
False
True or false:
ou can use a recursive algorithm to find the largest element in an array.
True
True or false:
To design a recursive function, you must determine the limiting conditions.
True
A definition in which something is defined in terms of a smaller version of itself is called a(n) ____ definition.
recursive
The ____ case is the case for which the solution to an equation is obtained directly.
base
int foo(int n)
{
if (n == 0)
return 0;
else
return n + foo(n - 1);
}
Consider the accompanying definition of a recursive function. Which of the statements represents the base case?
Statements in lines 3 and 4
int foo(int n)
{
if (n == 0)
return 0;
else
return n + foo(n - 1);
}
Consider the accompanying definition of a recursive function. Which of the statements represents the general case?
Statements in lines 5 and 6
void printNum(int num)
{
if (n < 0)
cout «_space;“Num is negative” «_space;
endl;
else if (num == 0)
cout «_space;“Num is zero” «_space;endl;
else
{
cout «_space;num «_space;” “;
printNum(num – 1);
}
}
Consider the accompanying definition of a recursive function. Which of the statements represent the base case?
Statements in lines 3-6
void printNum(int num)
{
if (n < 0)
cout «_space;“Num is negative” «_space;
endl;
else if (num == 0)
cout «_space;“Num is zero” «_space;endl;
else
{
cout «_space;num «_space;” “;
printNum(num – 1);
}
}
Consider the accompanying definition of a recursive function. Which of the statements represent the general case?
Statements in lines 7-11
int recFunc(int num)
{
if (num >= 10)
return 10;
else
return num * recFunc(num + 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout «_space;recFunc(8) «_space;endl;
720
int recFunc(int num)
{
if (num >= 10)
return 10;
else
return num * recFunc(num + 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout «_space;recFunc(10) «_space;endl;
10