Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 13

Chapter 13

Recursion

This activity contains 10 questions.

Question 1.

We have n people in a room, where n is an integer greater than or equal to 2. Each person shakes hands once with every other person.

Which of the following recursive functions correctly computes the total number of handshakes for n people in a room?

To get you started, if there are only one or two people in the room, then:

handshake(1) = 0
handshake(2) = 1

If a third person enters the room, she must shake hands with each of the two persons already there. Add these two handshakes to the previous number of handshakes and we get:

handshake(3) = handshake(2) + 2

 
End of Question 1


Question 2.

In a recursive function such as:

void writeVertical(int n)
{
	int num;

	if (n<10) {
		cout << n << endl;
	}
	else {
		num = n % 10;
		writeVertical( n / 10);
		cout << num << endl;
	}
}

Each recursive call to writeVertical produces a new local variable "num". What mechanism makes this possible?
 
End of Question 2


Question 3.
Why does a recursive function with an incorrect base case result in a stack overflow error message?

 
End of Question 3


Question 4.

The following are two functions for computing the nth Fibonacci number, where Fib(0) = 1, Fib(1) = 1, and Fib(n) = Fib(n-1) + Fib(n-2) when n >= 2.

int recursiveFib(int n)
{
	if (n<0) return 0;
	else if (n==0) return 1;
	else if (n==1) return 1;
	else {
		return recursiveFib(n-1) + 
			   recursiveFib(n-2);
	}
}

int iterativeFib(int n)
{
	int last, secToLast, cur, i;

	if (n<0) return 0;
	else if (n==0) return 1;
	else if (n==1) return 1;
	else {	
		last = 1;
		secToLast = 1;
		cur = 2;
		for (i=2; i<=n; i++) {
			cur = last + secToLast;
			secToLast = last;
			last = cur;
		}
		return cur;
	}
}

What will be the performance difference between the two functions?
 
End of Question 4


Question 5.

Given the following recursive function for factorial, how would this be invoked to correctly compute the factorial for number n?

long factorial(long n, long current_solution)
{
	if (n==1) return current_solution;
	current_solution = n * current_solution;
	return (factorial(n-1, current_solution));
}
 
End of Question 5


Question 6.
What will the following function output when given the input of "neat!" ?

void MysteryFunction()
{
  char c;
  cin >> c;
  if (c!='!') MysteryFunction();
  cout << c;
  return;
}

 
End of Question 6


Question 7.
Which of the following functions recursively sums all numbers in the array?

 
End of Question 7


Question 8.
Which of the following functions recursively determines if the input string is a palindrome?

 
End of Question 8


Question 9.

What will the following program output?

// Prototypes
void DoSomething(int arr[], int target, int &arrLength);
void DoSomething2(int arr[], int current, int target, int &arrLength);
void DoSomething3(int arr[], int target, int &arrLength);

// Function implementation
void DoSomething(int arr[], int target, int &arrLength)
{
	int i=0;
	DoSomething2(arr, i, target, arrLength);
}

void DoSomething2(int arr[], int current, int target, int &arrLength)
{
	if (current == arrLength) return;
	if (arr[current] == target) {
		DoSomething3(arr, current, arrLength);
		arrLength--;
	}
	else {
		DoSomething2(arr, current+1, target, arrLength);
	}
}

void DoSomething3(int arr[], int current, int &arrLength)
{
	if (current>=arrLength-1) return;
	arr[current] = arr[current+1];
	DoSomething3(arr, current+1, arrLength);
}

int main()
{
	int arr[] = {15, 3, 21, 7, 55};
	int arrLength = 5;

	DoSomething(arr, 3, arrLength);
	for (int i=0; i< arrLength; i++) {
		cout << arr[i] << ", ";
	}
	cout << endl;
	return 0;
}
 
End of Question 9


Question 10.

What will the following code output?

// Prototypes
void DoSomething(int arr[], int arrLength);
void DoSomething2(int arr[], int current, int arrLength);

// Function implementation
void DoSomething(int arr[], int arrLength)
{
	int i=0;
	DoSomething2(arr, i, arrLength);
}

void DoSomething2(int arr[], int currentIndex, int arrLength)
{
	int temp;
	
	if (currentIndex >= arrLength-1) return;
	DoSomething2(arr, currentIndex+1, arrLength-1);
	temp = arr[currentIndex];
	arr[currentIndex] = arr[arrLength-1];
	arr[arrLength-1] = temp;
}

int main()
{
	int arr[] = {15, 3, 21, 7,4, 55};
	int arrLength = 6;

	DoSomething(arr, arrLength);
	for (int i=0; i< arrLength; i++) {
		cout << arr[i] << ", ";
	}
	cout << endl;
	return 0;
}
 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page