Recursion is the process of a method calling itself. This may seem like a strang
ID: 3536846 • Letter: R
Question
Recursion is the process of a method calling itself. This may seem like a strange thing to do, but it is a way to implement the bottom-up approach to many common problems. Provide an example of a problem in which the solution would be using such a method. Ensure that the problem has multiple levels to utilize the recursion method in its solution.
A pointer is a variable that may contain a memory cell address. The term pointer is used because memory cell addresses %u201Cpoint to%u201D a location in which data values reside. Describe at least four pointer types and examples of their usage in a program to address a solution to a problem.
Explanation / Answer
Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our method will never finish. This might be true is some cases, but in practise we can check to see if a certain condition is true and in that case exit (return from) our method. The case in which we end our recursion is called a base case . Additionally, just as in a loop, we must change some value and incremently advance closer to our base case.
Consider this function.
void myMethod( int counter)
{
if(counter == 0)
return;
else
{
System.out.println(""+counter);
myMethod(--counter);
return;
}
}
This recursion is not infinite, assuming the method is passed a positive integer value. What will the output be?
Consider this method:
void myMethod( int counter)
{
if(counter == 0)
return;
else
{
System.out.println("hello" + counter);
myMethod(--counter);
System.out.println(""+counter);
return;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.