Question

In: Computer Science

Consider the function computational as implemented below. (a) Describe what the function does. (b) What is...

Consider the function computational as implemented below.
(a) Describe what the function does.
(b) What is the output of this function.
(c) Explain the recursive action. (While describing the recursive action define what recursion is and how does recursion work.)


int computational(int n){
if (n < 10) && (n > -10))
return 1;
else
return 1 + computational(n/10);
}
A good variant expression is “the number of digits in n,” with the threshold of 1.

Solutions

Expert Solution

Consider the following function:

(a) Describe what the provided function does:

• The above function counts the number of digits in input integer n.

• The provided function works recursively to find the number of digits in positive or negative input.

If the input is 846 then the output will be 3.

If the input is 9782536 then the output will be 7.

If the input is 8 then the output will be 1.

If the input is -26 then the output will be 2.

(b) Output of the function:

Output:

(c) Explain the function's recursive action:

• Recursion:

• The process or technique in which a function calls or references to itself is called recursion.

• This procedure enables function code to repeat several times.

• How recursive function works:

• Recursive function generally have two cases:

Base case: Base case returns some static value to the function.

Recursive case: Recursive case recursively calls the function.

• General structure of recursive function:

void myfunction()

{

myfunction(); // Function calls or references to itself.

}

int main()

{

myfunction();

}

Recursive action of provided function:

Let's consider the input n = 100

int computational(int n) // n = 100

{

if (n < 10) && (n > -10)) // if condition is false go to else

return 1;

else

return 1 + computational(n/10); // 1 + computational(100/10)

}

call function computational() again:

int computational(int n) // n =10 ( n/10 = 100/10 = 10)

{

if (n < 10) && (n > -10)) // if condition false go to else

return 1;

else

return 1 + computational(n/10); // 1 + computational(10/10)

}

call function computational() again:

int computational(int n) // n =1 ( n/10 = 10/10 = 1)

{

if (n < 10) && (n > -10)) // if condition is true and return 1

return 1;

else

return 1 + computational(n/10);   

}

Function call 3: computational(1) = 1

Function call 2: computational(10) = 1 + 1 = 2

Function call 1: computational(100) = 1 + 2 = 3

Output: 3


Related Solutions

What is a [cumulative] distribution function and what does it describe?
What is a [cumulative] distribution function and what does it describe?
b.   The production function.    i.   What is the production function?    ii.   What questions does...
b.   The production function.    i.   What is the production function?    ii.   What questions does it help a business answer?    iii.   How would you approach defining your company production function?    iv.   What information would you need?    v.   Where would you get that info? c. Short    i.   List and explain the 3 Short Run Production Relationships discussed in the slides and audio.    ii.   Explain who in a business would care about them and how they...
What is a production function? What does it describe? How is it used?
What is a production function? What does it describe? How is it used?
Describe the function of the sodium/potassium ATPase. a. What does the protein/molecule itself do?(3 points) b....
Describe the function of the sodium/potassium ATPase. a. What does the protein/molecule itself do?(3 points) b. How does the function of this one protein/molecule play a role in homeostasis of the whole body? Describe at least three specific, unique ways.
Consider the function below. (If an answer does not exist, enter DNE.) h(x) = (x +...
Consider the function below. (If an answer does not exist, enter DNE.) h(x) = (x + 1)9 − 9x − 2 (a) Find the interval of increase. (Enter your answer using interval notation.) Find the interval of decrease. (Enter your answer using interval notation.) (b) Find the local minimum value(s). (Enter your answers as a comma-separated list.) Find the local maximum value(s). (Enter your answers as a comma-separated list.) (c) Find the inflection point. (x, y) = Find the interval...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = 1 2...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = 1 2 x4 − 4x2 + 2 (a) Find the interval of increase. (Enter your answer using interval notation.) (−2,0)∪(2,∞)    Find the interval of decrease. (Enter your answer using interval notation.) (−∞,−2)∪(0,2)    (b) Find the local minimum value(s). (Enter your answers as a comma-separated list.) DNE    Find the local maximum value(s). (Enter your answers as a comma-separated list.) 2    (c) Find the...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = 1 2...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = 1 2 x4 − 4x2 + 6 (a) Find the interval of increase. (Enter your answer using interval notation.) Find the interval of decrease. (Enter your answer using interval notation.) (b) Find the local minimum value(s). (Enter your answers as a comma-separated list.) Find the local maximum value(s). (Enter your answers as a comma-separated list.) (c) Find the inflection points. (x, y) =    (smaller x-value)...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = earctan(7x) (a)...
Consider the function below. (If an answer does not exist, enter DNE.) f(x) = earctan(7x) (a) Find the vertical asymptote(s). (Enter your answers as a comma-separated list.) Find the horizontal asymptote(s). (Enter your answers as a comma-separated list.) (b) Find the interval where the function is increasing. (Enter your answer using interval notation.) Find the interval where the function is decreasing. (Enter your answer using interval notation.) (c) Find the local maximum and minimum values. (d) Find the interval where...
What does the Schechter function describe? Describe the significance of each of the two fitting parameters....
What does the Schechter function describe? Describe the significance of each of the two fitting parameters. What does the de Vaucouleurs law describe? Describe the significance of each of the two fitting parameters.
At Zappos, the management function of “leading” is implemented in a very specific way. What are...
At Zappos, the management function of “leading” is implemented in a very specific way. What are the characteristics that are valued by Zappos in terms of management and leadership?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT