A recursive process is a process that calls itself.
To calculate 1·2 ·3 ºn, here is a process that works:
Step 2 is called the Recursive Step, the place where the process calls itself.
Here it is as a function in C++:
int Multiply1ThruN (int N)
{
int answer = 0;
if (N == 1)
answer = 1; //Exit step
else if (N > 1)
answer = Multiply1ThruN (N - 1) * N; //Recursive step
return answer;
}
If we call the above function with the statement:
cout << Multiply1ThruN (3) << endl;
then what really happens?
The computer maintains in memory a sequential array called The Stack. Function calls and their arguments are placed on The Stack until they can be resolved into a return value.
cout << Multiply1ThruN (3) << endl; results in the following sequence of events:
| Multiply1ThruN (3) |
| Multiply1ThruN (3) |
| Multiply1ThruN (2) |
| Multiply1ThruN (3) |
| Multiply1ThruN (2) |
| Multiply1ThruN (1) |
| Multiply1ThruN (3) |
| Multiply1ThruN (2) |
| 1 |
| Multiply1ThruN (3) |
| 2 |
| 6 |
Here is a recursive version of a simple sorting method, given a vector of positive ints in slots 0 through N and a place in the vector to start sorting:
The plan:
If there is just one number to sort
else
The C++ code looks like:
void RecursiveSort (vector <int>& numbers, //List to sort
int startPlace) //Place to start sorting
{
//Purpose: This function recursively sorts the list of numbers from
// startPlace on down.
if (startPlace < numbers.size ())
{
//Find the position of the smallest number AFTER the starting place.
int posOfSmallest = FindPositionOfSmallest (numbers, startPlace + 1);
//If this number is smaller than the number at the starting place,
// then swap it to the starting place.
if (numbers [posOfSmallest] < numbers [startPlace])
SwapNumbers (numbers [startPlace], numbers [posOfSmallest]);
//Then sort the rest of the list.
RecursiveSort (numbers, startPlace + 1);
}
}
int FindPositionOfSmallest (vector<int>& numbers, int startPlace)
{
int smallest = numbers [startPlace];
int smallPosition = startPlace;
for (int n = startPlace; n < numbers.size (); n++)
{
if (smallest > numbers [n])
{
smallest = numbers [n];
smallPosition = n;
}
}
return smallPosition;
}
void SwapNumbers (int& one, int& two)
{
int temp = one;
one = two;
two = temp;
}
int PowerFn (int n, int exponent)
{
int returnVal;
if (exponent == 0)
returnVal = 1; //Exit step, n = 0
else
returnVal = n * PowerFn (n, exponent - 1); //Recursive step
return returnVal;
}
*****
****
***
**
*
void PrintTriangle (int n)
{
if (n == 1)
{
//Exit step
cout << "*" << endl;
}
else
{
//Now the recursive step:
//Print the first row of n *'s
// Then print the remaining triangle with n - 1 rows
// (same task one level down).
for (int place = 1; place <= n; place++)
cout << "*";
cout << endl;
PrintTriangle (n - 1);
}
}
int GetAndAddNumbers (int n)
{
int input, returnVal;
if (n == 1)
{
//Exit step
cout << "Enter a number";
cin >> input;
returnVal = input;
}
else
{
//Now the recursive step:
//Get the first number, add it to the sum of the next n - 1
// numbers (same task one level down).
cout << "Enter a number";
cin >> input;
returnVal = input + GetAndAddNumbers (n - 1);
}
return returnVal;
}
int Sequence (int n)
{
int returnVal;
//There are two exit steps here, n = 1 and n = 2
if (n == 1)
returnVal = 1;
else if (n == 2)
returnVal = 2;
else
//The Recursive Step requires the last two values:
// the same task one level down, (n - 1), and also the same task
// two levels down, (n - 2).
returnVal = 3 * Sequence (n - 1) + 2 * Sequence (n - 2);
return returnVal;
}
The place: the lines of the Continental Army under General Washington.
The problem: You get up in the morning as usual, and to your great surprise, the year is 1776. CSM doesn't even exist and you haven't been born. (But I'll overlook this if you will.) Anyway, the Americans are in the middle of fighting a war to get independence from the bloody British.
The bloody British are shelling the American position with a large cannon. As the General's chief of intelligence (quite an achievement for someone who hasn't been born!), you sneak into the bloody British camp to see how many cannon balls the bloody British have for that pesky cannon. You see that there is one stack of cannon balls arranged in layers. There is one ball at the top of the stack, 4 in the next layer, 9 in the next layer, then 16, and so on. The bloody British are getting suspicious, so you only have time to count the layers (there are 20) before you have to get out of there.
When you get back to the American lines you want to impress General Washington, your boss. So you whip out your laptop computer, bring up your Poor Richard's C++ compiler, and write a recursive function to compute the solution. The General just won't be impressed with a for loop. (You are probably wondering where power for a computer could come from in 1776. The laptop is plugged into a lightning-attracting kite - learned that one from Ben Franklin.)
What was the function?
Write a recursive function that returns the base 5 value of a given number. NOTE: In base 5 no digits can be more than 4. If the user enters such a thing, your program should catch it.
Write a recursive function to find the GCD of any two given positive integers.