Question

In: Computer Science

Recursion Part //1. Write out the output for eaching of following: /* Consider this function. void...

Recursion Part

//1. Write out the output for eaching of following:

/* 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?

b. Consider this method:

void myMethod( int counter)

{

if(counter == 0)

return;

else

{

System.out.println("hello" + counter);

myMethod(--counter);

System.out.println(""+counter);

return;

}

}

If the method is called with the value 4, what will the output be? Explain.

c.

public class Recursion {

public static void mystery1(int a, int b) {

if (a <= b) {

int m = (a + b) / 2;

System.out.print(m + " ");

mystery1(a, m-1);

mystery1(m+1, b);

}

}

public static void mystery2(int n) {

if (n > 0) {

System.out.print(n + " ");

mystery2(n-2);

mystery2(n-3);

System.out.print(n + " ");

}

}

public static void mystery3(int n) {

if (n == 0 || n == 1) return;

mystery3(n-2);

System.out.print(n + " ");

mystery3(n-1);

}

public static String mystery4(int n) {

if (n <= 0) return "";

return mystery4(n-3) + n + " " + mystery4(n-2) + n + " ";

}

public static void main(String[] args) {

int N = 5;

mystery1(0, N);

System.out.println();

mystery2(N);

System.out.println();

mystery3(N);

System.out.println();

System.out.println(mystery4(N));

}

}

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have executed the above three codes to fetch their execution and output. Although an explanation is asked for only question b), if have provided the explanation for all the 3 codes. The explanation for code 3 is quite difficult as it undergoes a lot of recursive calls and returns, I have still tries my best to give you an idea of how the recursive calls occur.

Question a)

The program prints the list of numbers starting from the entered value (N) to 1, decreasing order with every number in new line as the output. However, if the integer passed is 0 then it does not print anything.

Explanation:

When myMethod() is called with 10 then:

· If condition on line 6 is checked, it is false

· Line 10 is executed, 10 (passed parameter) is printed and recursion happen at line 11 where myMethod is called using 9 as parameter.

· The same process is repeated till the passed parameter during recursion is 0. Once the counter becomes 0, the recursion ends.

Executable code:

Output: (if the passed integer is not 0)

Output: (if the passed integer is 0)

(blank output)

Program b)

The program gives an output where the first 4 lines contain hello appended to the value passed and the value is decremented. From the fifth line we print only the numbers from 0 increasing upto the number that was passed.

Explanation:

1. We call myMethod() using the parameter 4.

2. Line 6 is checked, the condition is false so the else part is executed.

3. Now, Line 10 is executed i.e Hello appended by 4 (passed parameter is printed

4. Now, recursion begins by passing 3 (decreasing passed parameter by 1) as the new parameter.

5. Steps 1-4 are repeated until the passed parameter becomes 0

6. Once the counter is 0 then Line 12 for each recursion call is executed and thus the numbers will be printed from 0 to 3

For input value: 4

Executable code:

Output:

Program c)

Explanation: If we understand the logic of one mystery() method, then all the other methods can be similarly understood.

1. In line 34, we call mystery(0,N) where N=5.

2. Condition on Line 4 is checked. It is true and thus m=(0+5)/2 which gives m=2

3. We print the value of m and the recursion for line 7 begin by passing parameters (0,1)

4. Now the above three steps are repeated again and 0 is printed beside 2. Method is further called using (0,-1) as parameters.

5. The condition fails in the next recursive call and line 8 for all recursive calls execute following the same process.

6. The recursion of line 8 prints 1 4 3 5 after four calls and the control is shifted back to the main() as the function call that started at line 34 is completed.

7. Further lines of code are executed and the final output can be seen in the image.

Executable code:

Output:


Related Solutions

/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
1. Consider a firm with the following production function: Q = KL1/2 (a) Consider an output...
1. Consider a firm with the following production function: Q = KL1/2 (a) Consider an output level of Q = 100. Find the expression of the isoquant for this output level. (b) Find the marginal product of labor, MPL. Is it increasing, decreasing, or constant in the units of labor, L, that the firm uses? (c) Find the marginal product of capital, MPK. Is it increasing, decreasing, or constant in the units of capital, K, that the firm uses? (d)...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d...
-> > Use recursion to write a C++ function for determining if a strings has more...
-> > Use recursion to write a C++ function for determining if a strings has more vowels than consonants. Please include the pseudo code so that I can understand better with simple English as much as possible.
Consider the following production function: ? = ? ?? ?? ? , where Y denotes output,...
Consider the following production function: ? = ? ?? ?? ? , where Y denotes output, and K denotes capital input, L denotes and labor input, and D denotes land input. There is a firm that uses this technology to produce output by choosing the quantity of each input a. Under what conditions does this production function exhibit constant returns to scale? b. Suppose the firm is perfectly competitive so it takes input prices as given (r is the rental...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
1/ Growth Rates of Capital and Output Consider the following production function: Yt = F(Kt ,...
1/ Growth Rates of Capital and Output Consider the following production function: Yt = F(Kt , Lt) = K 1 2 t L 1 2 t Assume that capital depreciates at rate ? and that savings is a constant proportion s of output: St = sYt Assume that investment is equal to savings: It = St Finally, assume that the population is constant: Lt = Lt+1 = L 1. The production function above expresses output as a function of capital...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT