Access over 20 million homework & study documents
search

3 Given the following recursive function int myFactorial (int in

Content type

User Generated

Rating

Showing Page:
1/2
3. Given the following recursive function:
int myFactorial (int integer)
{
if (integer == 1)
{
return 1;
}
else
{
return (integer * (myFactorial (integer 1));
}
}
Show all the calls to the function and what each one
returns to its predecessor.
int result = myFactorial (5);
Solution
when
int result = myFactorial(5);
is executed \"myFactorial\" is called with input 5
As 5 is not equal to 1
myFactorial(5) returns
5*myFactorial(5-1)
=5*myFactorial(4)
result=5*myFactorial(4)

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/2

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

3. Given the following recursive function: int myFactorial (int integer) { if (integer == 1) { return 1; } else { return (integer * (myFactorial (integer – 1)); } } Show all the calls to the function and what each one returns to its predecessor. int result = myFactorial (5); Solution when int result = myFactorial(5); is executed \"myFactorial\" is called with input 5 As 5 is not equal to 1 myFactorial(5) returns 5*myFactorial(5-1) =5*myFactorial(4) result=5*myFactorial(4) As 4 is not equal to 1 myFactorial(4) returns 4*myFactorial(4-1) =4*myFactorial(3) result=5*4*myFactorial(3) As 3 is ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Great! 10/10 would recommend using Studypool to help you study.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4