Jump to content
• Advertisement

#### Archived

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

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

This topic is 6383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

##### Share on other sites
Advertisement
numberone factorial

#### Share this post

##### Share on other sites
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

##### Share on other sites

oops!

--Rick
TribesPlayers.com

#### Share this post

##### Share on other sites
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 thats correct, dont go using a compiler!

Ozz.

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

#### Share this post

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### 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

##### Share on other sites
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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5
• Advertisement

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631768
• Total Posts
3002241
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!