Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

C++ Recursion


  • Please log in to reply
2 replies to this topic

#1 martintaylor1635

martintaylor1635

  •  Avatar image
  • Members
  • 2 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Glasgow, Scotland
  • Local time:08:50 PM

Posted 11 March 2021 - 08:39 AM

Hi,

 

I'm enrolled on a 3rd year BSc Computing Science degree and currently undertaking a module 'Algorithms & Collections'. I have been given the task to map out the activation records for the following piece of code:

int mystery(int n)
{
 
   // Base Case
   if (n <= 0) { return 0; }

   // Recursive (general) case
   return mystery( n / 2 ) + 1;

}

However, what I am confused about, is how the answer is 5?

 

If anyone could help me understand this it would be greatly appreciated!

 

Edit: My apologies, I meant, I can't seem to get why the answer would be 5 upon invoking mystery(20):lmao:


Edited by martintaylor1635, 11 March 2021 - 01:17 PM.


BC AdBot (Login to Remove)

 


#2 RecursiveNerd

RecursiveNerd

    Bleepin' Unicorn


  •  Avatar image
  • Malware Response Team
  • 2,664 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Kentucky, USA
  • Local time:03:50 PM

Posted 12 March 2021 - 03:23 PM

Greetings martintaylor,

If you write out the entire call chain (i.e. how the code would execute), you would get something like this:
mystery(20/2) + 1 =>

(mystery (10/2) + 1) + 1 =>

((mystery(5/2) + 1) + 1) + 1 =>

(((mystery(2/2) + 1) + 1) + 1) + 1 =>

((((mystery(1/2) + 1) + 1) + 1) + 1) + 1 =>

((((0 + 1) + 1) + 1) + 1) + 1
Bascially, each recursive call simply gets replaced with (mystery(N/2) + 1). Following this through, 1+1+1+1+1 = 5. Since your code shows int types, you're doing integer division. Integer division always results in 0 if the divisor (bottom number - in your case 2) is greater than the dividend.
Regards,

Jake (AKA RecursiveNerd)

#3 martintaylor1635

martintaylor1635
  • Topic Starter

  •  Avatar image
  • Members
  • 2 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Glasgow, Scotland
  • Local time:08:50 PM

Posted 12 March 2021 - 03:40 PM

Greetings martintaylor,

If you write out the entire call chain (i.e. how the code would execute), you would get something like this:

mystery(20/2) + 1 =>

(mystery (10/2) + 1) + 1 =>

((mystery(5/2) + 1) + 1) + 1 =>

(((mystery(2/2) + 1) + 1) + 1) + 1 =>

((((mystery(1/2) + 1) + 1) + 1) + 1) + 1 =>

((((0 + 1) + 1) + 1) + 1) + 1
Bascially, each recursive call simply gets replaced with (mystery(N/2) + 1). Following this through, 1+1+1+1+1 = 5. Since your code shows int types, you're doing integer division. Integer division always results in 0 if the divisor (bottom number - in your case 2) is greater than the dividend.

 

 

Thank you Jake,

 

I really appreciate your time! That helped me a lot to understand it!

 

Regards,

Martin






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users