Archived

This topic is now archived and is closed to further replies.

Ozz

C++ Recursion Quiz : Some Light Relief...

Recommended Posts

Ozz    122
Can you figure out what 'dosomething' does without a compiler
    
int dosomething(int numberone)
{
     if(numberone == 1) {return 1;}
     
     if(numberone >= 1) 
     {
         return (numberone * dosomething(numberone-1));
     }
}
  
Just a little light relief! Regards, Ozz. Edited by - Ozz on January 27, 2001 2:34:52 PM

Share this post


Link to post
Share on other sites
ncsu121978    1344
nope.......that is the wrong answer
it will ALWAYS return 1
why ?
  
if (numberone = 1) { return 1; }

that is why
unless of course you mean to have the double equal marks there in which case it would be numberone factorial


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
We are creating a Multi-player space strategy/shoot-em-up/RPG game.
Development is well under way and we do have a playable demo.
Always looking for help.
Digital Euphoria Soft

Share this post


Link to post
Share on other sites
Ozz    122
Mwahh-ha-ha, I double tricked you! It was actually an error on my part, it should be 'if(numberone == 1) {return 1;}' and yes, 'dosomething' is infact 'factorial'.

How about this one:

    

int dosomething(int number)
{
if((number == 1) || (number == 2)) return 1;

return(dosomething(number-1) + dosomething(number-2);
}



I think that`s correct, don`t go using a compiler!

Ozz.

Edited by - Ozz on January 27, 2001 2:35:39 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
number = 2;

dosomething(number)
{
if(number == 1)return 1;

if(number >= 1)
return(number * dosomething(number1);
}

first time through we skip the first if
and call dosomething from inside the first
layer dosomething(2-1)(second layer now)
that returns 1 and the first layer multiplies
it by number (2).. 2*1=2 and returns 2
to the initial call

is that wrong? let me know why im
still trying to understand recursion..

Share this post


Link to post
Share on other sites
Ozz    122
You have the idea, but I think you need a hint: the recursive function generates a very famous and very intesting sequence of numbers.

Share this post


Link to post
Share on other sites
abrunyee    122
Just being picky, here. I''ve not tried compiling it, but wouldn''t it fail as not all control paths actually return a value?

E.g. If you called dosomething(-2)...

Share this post


Link to post
Share on other sites
the second code generates the kth number from the Fibonacci series:

1, 1, 2, 3, 5, 8, ...

which is created by adding two most previous numbers:

Fibonacci(6) = 3 + 5 = 8
Fibonacci(7) = 5 + 8 = 13



"Well, then, - the Cat went on - you see, a dog growls when it''s angry, and wags its tail when it''s pleased. Now I growl when I''m pleased, and wag my tail when I''m angry. Therefore I''m mad."

Share this post


Link to post
Share on other sites
karmalaa    122
Sorry for my late answer...

As concern with the first "quiz"...

The function implements a "not-so-exact" recursive computation of the factorial of ''numberone''.

I called it "not-so-exact" for two reasons.

First, if called with a integer actual parameter equal to zero, the function won''t return anything (clever compilers issue a warning for this ).

Second, ''int'' values can be negative. The factorial of a negative number is NOT defined.

To solve this secondary problem the programmer should either add a piece of "valid argument checking" code or (better) set the formal argument type to "unsigned int" (and the return value type, too).

Just my $0.02...





[home page] [e-mail]

---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"

Share this post


Link to post
Share on other sites
Cobra    122
Yeah, it generates numbers from the Fibonacci series.

( I really shoulda got that one first! hehehe.. ahh well. nevermind )

~Cobra~

Share this post


Link to post
Share on other sites
rileyriley    235
quote:
Original post by abrunyee

Just being picky, here. I''ve not tried compiling it, but wouldn''t it fail as not all control paths actually return a value?

E.g. If you called dosomething(-2)...


You''d get stuck in an infinite loop, for sure, but it would still compile. dosomething(-2) _does_ have a return value, it''s doSomething(-3) + doSomething(-4).

anyway my point is that it would _not_ fail to compile.

/riley

Share this post


Link to post
Share on other sites