Question

In: Computer Science

Guided Assignment Problem 3: Call Stack Your Tasks: Answer the following questions. Include a function stack...

Guided Assignment Problem 3: Call Stack

Your Tasks:

Answer the following questions. Include a function stack chart/diagram for each question to verify your answers.

  1. Use a call stack to explain the result of calling example(3).

    int example(int n) {

      if (n == 0)

    return 0;

      else

    return example(n – 1) + n * n * n;

    }

  2. Use a call stack to explain how many time(s) the method factorial invokes itself with an argument of 5 of factorial(5).

    int factorial(int n) {

      if (n == 0)

        return 1;

      else

        return (n * factorial(n – 1));

    }

  3. Include the following items in the same Word file for the final submission:
    • Your responses to the two tasks listed above

Solutions

Expert Solution

1. example(3)

This function finally returns 36 that can be seen from the below diagram:

example(3) calls 2, 2 calls 1, and 1 calls 0, finally example(0) returns 0 and the values are computed as shown in the figure above. Then example(1) returns 1, example(2) adds 8 to 1 and returns 9, and finally example(3) adds 9 to 27 and returns 36 as the final ans.

2. Factorial(5)

After factorial(5) is invoked, method factorial invokes itself 5 times more as shown:

When call reaches factorial(0), it starts to return values. 1 is returned to factorial(1), which again returns 1 to factorial(2), it then returns 2 to factorial(3), which returns 6 to factorial(4), which returns 24 to factorial(5), which then finally returns the final answer as 24*5 = 120


Related Solutions

Your Tasks: After reading the case study, answer the following questions: Describe in your own words...
Your Tasks: After reading the case study, answer the following questions: Describe in your own words the meaning with examples of the following terms:- - Unconscious Bias - Stereotyping, Prejudice and Discrimination. (10 marks – 150 words) Identify TWO theories that you have learned in this unit and then relate their relevance and applicability to the given case study. (5 marks – 200 words) Here the date, take two theories from it http://www.mediafire.com/file/vop79sq3pquzgyb/Lecture+5.pptx/file Based on your understanding of the case...
PLEASE READ CAREFULLY AND EXPLAIN YOUR ANSWER IF POSSIBLE (JAVASCRIPT ONLY) Write out the call stack...
PLEASE READ CAREFULLY AND EXPLAIN YOUR ANSWER IF POSSIBLE (JAVASCRIPT ONLY) Write out the call stack for this program if x is 5. function factorial(x) { if (x === 0) { return 1; } return x * factorial(x-1); } console.log(factorial(x)); Use "not in function" if not in a function, and put the function name along with the arguments if in a function. Not in function in function_name(arg, ...) ...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8....
Answer the following questions using the Toyota Corporation as the focus of your assignment 1) Analyze...
Answer the following questions using the Toyota Corporation as the focus of your assignment 1) Analyze how your company is leveraging social media in their communications strategy. List which social media platforms are currently being used by your company (which may include profiles for specific brands or products), and provide an example demonstrating how the company utilizes social media to offer sensory experiences, engages customers socially, and for industrious purposes. 2) Imagine your company wants to run a campaign to...
Describe the structure and function of the chloroplast and mitochondrion. Include in your answer: a. a...
Describe the structure and function of the chloroplast and mitochondrion. Include in your answer: a. a description of each compartment, the membranes, and the localized activities of both organelles b. the special properties of chlorophyll and the mechanism of action of the light harvesting complex in the chloroplast c. the sequences of events in both electron transport chains from electron sources to the electron sinks and the roles of NADH and NADPH
Directions: Complete the assignment. Clearly label each answer. Your answers for this assignment must include reasons;...
Directions: Complete the assignment. Clearly label each answer. Your answers for this assignment must include reasons; simply stating the answer without justification will earn partial credit. (32points) The gestation period of gray squirrels in captivity is listed as 44 days. It is recognized that the potential life span of animals is rarely attained in nature, but the gestation period could be either shorter or longer. Suppose the gestation period of a random sample of 49 squirrels living in the wild...
Assignment Details Answer the following 2 questions in your main post: Looking at probability in research,...
Assignment Details Answer the following 2 questions in your main post: Looking at probability in research, which do you believe is the best approach between classical, empirical, and subjective? Explain. When working with research, would you rather use conditional or joint probability? Explain.
Answer the following questions based on the given c file: #include #include #include    int c...
Answer the following questions based on the given c file: #include #include #include    int c = 0; void *fnC() {     int i;     for(i=0;i<10;i++)     {   c++;         printf(" %d", c);     }       } int main() { int rt1, rt2;   pthread_t t1, t2; int trial_count = 0; // For every trial, lets zero out the counter and run the count routine “twice”     // as threads that can be scheduled onto independent cores instead of running     // in sequence. for...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection matrix. Giving three 3D points v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0),you are required to transform these points to the camera/view/monitor coordinates system, and draw a lined triangle based on them get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): using the giving parameter, build a projection matrix, and return it. Here is what I have so far. Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,...
Use the table below to answer the questions below. The following prices are for call and...
Use the table below to answer the questions below. The following prices are for call and put options on a stock priced at $50.25. The March options have 90 days remaining and the June options have 180 days remaining. In your profit answers below, assume that each transaction is scaled by 100, reflecting the size of option contracts. Calls Puts Strike March June March June 45 6.85 8.45 1.20 2.15 50 3.90 5.60 3.15 4.20 55 1.95 3.60 6.15 7.00...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT