Question

Can someone hand trace this basic recursive function? Please hand-trace and show me all the steps. I do not understand how this works.

int main() message(3); return e; void message (int times) cout <Message<< times<.n if (times >e) message(times 1); else cou

Expected output is:

The output is:

Message 3

Message 2

Message 1

Message 0

Message 0 is returning.

Message 1 is returning.

Message 2 is returning.

Message 3 is returning.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Hess melae (3 Printe d Pinledl n le me Magc (2) me e (O me me ka Retaning is Rehming(u one Recsive cal we ae cauin hlenme Mag

Explanation: Generally the recursive calls are stored in stack of computer memory. stack follows Lat In First Out strategy. First the method call Message (3) is stored in stack and consequently Message(2),,Message(1),Message(0). Then recursive calls stops because times=0. Now Message(0) is on top of the stack and message(3) is at bottom of stack. while returning functions the stack top Message(0) is invoked first and so on and finally Message(3) is invoked last.so the returning calls are in reverse order.

Add a comment
Know the answer?
Add Answer to:
Can someone hand trace this basic recursive function? Please hand-trace and show me all the steps....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 6) Show the output of the following program. You mus trace the code to show how...

    6) Show the output of the following program. You mus trace the code to show how you reached the answer. (5 points) #include <stdio.h> int x 18; int y = 5; int z ·e; void my first function) void my_second_function); int main() int ys y = x; if (z) my_first functionO else my-second-function(); x++ : if (z) my_first_function(); else my_second_function(); return e; void my_first_function) x+y); is %d\n", printf("The value of x+y in my-first-function() void my_second_function) ( x=100; is %d\n", x);...

  • This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int...

    This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);

  • IV. Function Tracing (20 points) Trace the following program. Show llof your work and the ound...

    IV. Function Tracing (20 points) Trace the following program. Show llof your work and the ound he output in the work including labeled boxes for user-defined functions, calculations, and the output in designated spaces. You should draw a box for each variable and show value updates d the output, Show all of your #include <iostream» using namespace std; void mickey (int sa, int b, int &c) void minnie(int u, int &v, int w); void pluto (int m, int n, int...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts]...

    Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts] Given the Fibonacci numbers, defined as: Fo 0, F1-1, F k Fk2 write or draw the recursive trace of the calculation of the 5th Fibonacci number (Fs 5) with the following Algorithm (which is based on linear recursion) Algorithm Fibonacci Linear (k) Input: a positive integer value k Output: a pair of Fibonacci numbers (F., F..) if k < 2 then R-1 return (R,...

  • a) Hand-trace the following program and determine and write down what is the output of the...

    a) Hand-trace the following program and determine and write down what is the output of the code.                b) Run the code and compare the program output to your hand-traced result obtained from part (a). #include <iostream> #include <iomanip> using namespace std; void f(); int x = 5; int main() {         cout << "x = " << x << endl;         int x = 6;         cout << "x = " << x << endl;         {                int...

  • Can someone please tell me why the calculation for earnings is not coming through at the...

    Can someone please tell me why the calculation for earnings is not coming through at the end of the program? Thank you. //This program should tell us about streaming services #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {    int choice, streams;    double earnings;    string song;    string artist;    string streamingService;       const int Tidal = 0.01250;    const int Amazon = 0.00402;    const int Apple_Music = 0.00735;    const int...

  • Can someone tell me what I am doing wrong? Its a cootie lab for C++. My...

    Can someone tell me what I am doing wrong? Its a cootie lab for C++. My compiler wont let it run but it succeeds. I dont know why it wont work, can someone help? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int roll, body, head, antennae, wings, stinger, legs, eyes; int diceRoll() {    int randnum = (rand() % 7); int r = randnum + 1; return r; } int addbody (int body) { if (body < 1)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT