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
C++ Recursion Quiz : Some Light Relief...
Can you figure out what 'dosomething' does without a compiler
nope.......that is the wrong answer
it will ALWAYS return 1
why ?
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
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
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:
I think that`s correct, don`t go using a compiler!
Ozz.
Edited by - Ozz on January 27, 2001 2:35:39 PM
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
January 27, 2001 04:20 PM
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..
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..
You have the idea, but I think you need a hint: the recursive function generates a very famous and very intesting sequence of numbers.
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)...
E.g. If you called dosomething(-2)...
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."
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."
"- To begin with, said the Cat, a dog's not mad. You grant that? - I suppose so, said Alice. - 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."
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"
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"
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement